Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

I am making an ai to detect not safe for work imagery and flag it and also add tags to…

  1. User

    I am making an ai to detect not safe for work imagery and flag it and also add tags to what it thinks it is. currently my dataset is just pictures, please make two python scripts to 1, train the model, and 2 to use the model and annotate test data

  2. Assistant

    Sure! Here are two Python scripts to help you train the model and use it to annotate test data. For simplicity, we'll use the popular deep learning framework TensorFlow and a pre-trained model called InceptionV3 for image classification.

    Script 1: Train the Model

    import tensorflow as tf
    from tensorflow.keras.preprocessing.image import ImageDataGenerator
    
    # Set parameters
    train_data_dir = 'path_to_train_data_directory'
    validation_data_dir = 'path_to_validation_data_directory'
    batch_size = 32
    epochs = 10
    
    # Create image data generator
    train_datagen = ImageDataGenerator(rescale=1.0/255.0,
                                       shear_range=0.2,
                                       zoom_range=0.2,
                                       horizontal_flip=True)
    validation_datagen = ImageDataGenerator(rescale=1.0/255.0)
    
    # Load and prepare training data
    train_data = train_datagen.flow_from_directory(train_data_dir,
                                                   target_size=(150, 150),
                                                   batch_size=batch_size,
                                                   class_mode='binary')
    
    # Load and prepare validation data
    validation_data = validation_datagen.flow_from_directory(validation_data_dir,
                                                             target_size=(150, 150),
                                                             batch_size=batch_size,
                                                             class_mode='binary')
    
    # Load pre-trained InceptionV3 model
    base_model = tf.keras.applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
    
    # Freeze initial layers
    for layer in base_model.layers:
        layer.trainable = False
    
    # Add custom classification layers
    model = tf.keras.models.Sequential()
    model.add(base_model)
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(256, activation='relu'))
    model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
    
    # Compile the model
    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
    
    # Train the model
    model.fit(train_data, steps_per_epoch=len(train_data), epochs=epochs,
              validation_data=validation_data, validation_steps=len(validation_data))
    
    # Save the trained model
    model.save('nsfw_model.h5')
    

    Make sure to replace 'path_to_train_data_directory' and 'path_to_validation_data_directory' with the actual paths to your train and validation data directories, respectively.

    Script 2: Use the Model and Annotate Test Data

    import tensorflow as tf
    
    # Load the trained model
    model = tf.keras.models.load_model('nsfw_model.h5')
    
    # Load and prepare test data
    test_data_dir = 'path_to_test_data_directory'
    test_datagen = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0/255.0)
    test_data = test_datagen.flow_from_directory(test_data_dir,
                                                 target_size=(150, 150),
                                                 batch_size=1,
                                                 class_mode='binary',
                                                 shuffle=False)
    
    # Predict classes for test data
    predictions = model.predict(test_data)
    
    # Add predicted labels and tags to test data
    test_data.reset()
    filenames = test_data.filenames
    labels = predictions > 0.5  # Set a threshold for classifying NSFW images
    
    # Write annotations to a text file
    with open('annotations.txt', 'w') as f:
        for i, filename in enumerate(filenames):
            label = 'NSFW' if labels[i] else 'Safe'
            f.write(f'{filename}: {label}\n')
    

    Replace 'path_to_test_data_directory' with the path to your test data directory. Running this script will generate an annotations.txt file that contains the filenames along with their predicted labels (NSFW or Safe).

    Remember to adjust the parameters according to your dataset and requirements. Also, make sure you have TensorFlow and any other necessary libraries installed in your environment.