在使用图像数据生成器时,如何使用t-SNE可视化图像和潜在空间表示?

时间:2018-12-03 19:12:54

标签: python keras classification visualization

我使用图像数据生成器使用增强数据训练深度学习模型。我想可视化image(pixel)和潜在空间(我的代码中的全局平均池化层的输出)中的数据,以了解其分布。我是t-SNE可视化的新手,并希望将其用于此目的。我可以看到有关将t-SNE与自动编码器一起用于MNIST数字而不是CNN的示例,尤其是在使用图像数据生成器时。有没有办法在我的代码中包含t-SNE以可视化图像和潜在空间数据表示?

# %% load libraries
from keras.models import Model
from keras.callbacks import ModelCheckpoint, TensorBoard
from keras.optimizers import SGD
from keras.applications.vgg16 import VGG16
from keras.layers import GlobalAveragePooling2D, Dense
from keras.preprocessing.image import ImageDataGenerator
# %% Loading the training data
img_width, img_height = 256, 256
train_data_dir = 'data/train'
test_data_dir = 'data/test'
epochs = 40
batch_size = 32 
num_classes= 2
vgg16_model = VGG16(weights='imagenet', include_top=False, input_shape=(256, 256, 3))
vgg16_model = Model(inputs=vgg16_model.input, outputs=vgg16_model.get_layer('block5_conv3').output)
x = vgg16_model.output
x = GlobalAveragePooling2D()(x)
predictions = Dense(num_classes, activation='softmax')(x)
vgg16_custom_model = Model(inputs=vgg16_model.input, outputs=predictions, name='vgg16_cnn')
vgg16_custom_model.summary()
# %%
#declaring image data generators, make sure to delcare shuffle=False
train_datagen = ImageDataGenerator(
        rescale=1./255,
        validation_split=0.2)
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
        # This is the target directory
        train_data_dir,
        target_size=(256, 256),
        batch_size=batch_size,
        class_mode='categorical',
        subset='training')
validation_generator = train_datagen.flow_from_directory(
        # This is the target directory
        train_data_dir,
        target_size=(256, 256),
        batch_size=batch_size,
        class_mode='categorical',
        subset='validation')
test_generator = test_datagen.flow_from_directory(
        test_data_dir,
        target_size=(256, 256),
        batch_size=batch_size,
        class_mode='categorical',shuffle=False)
#identify the number of samples
nb_train_samples = len(train_generator.filenames)
nb_validation_samples = len(validation_generator.filenames)
nb_test_samples = len(test_generator.filenames)
#check the class indices
train_generator.class_indices
validation_generator.class_indices
test_generator.class_indices
# %% compile and train for VGG16
sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)  
vgg16_custom_model.compile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy']) #optimizer=Adam() is another option
filepath = 'weights/' + vgg16_custom_model.name + '.{epoch:02d}-{val_acc:.4f}.hdf5'
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, 
                             save_weights_only=True, save_best_only=True, mode='max', period=1)
tensor_board = TensorBoard(log_dir='logs/', histogram_freq=0, batch_size=batch_size)
callbacks_list = [checkpoint, tensor_board]
vgg16_history = vgg16_custom_model.fit_generator(
      train_generator,
      steps_per_epoch=nb_train_samples // batch_size, #no. of training samples / batch_size
      epochs=epochs,
      validation_data=validation_generator,
      callbacks=callbacks_list,
      validation_steps=nb_validation_samples // batch_size, #no. of validation samples / batch size
      verbose=1)

0 个答案:

没有答案