├── 01.mnist_mpl.py ├── 02.load_model.py ├── 03.plot_acc_loss.py ├── 04.mnist_cnn.py ├── 05.cifar10_cnn.py ├── 06.mnist_lstm.py ├── 07.vgg-16.py ├── 08.conv_filter_visualization.py ├── 09.variational_autoencoder.py ├── 10.mnist_transfer_cnn.py ├── 11.Keras_with_tensorflow.py ├── 12.mnist_sklearn_wrapper.py ├── 13.finetune_InceptionV3.py ├── 14.sigle_autoencoder.py ├── 15.multi_autoencoder.py ├── 16.Convolutional_autoencoder.py ├── 17.Image_denoising.py ├── 18.GloVe_word_embeddings.py ├── 19.ImageDataGenerator.py ├── README.md ├── elephant.jpg ├── mnist-mpl.h5 ├── model-cnn.png ├── model.png ├── plot_acc_lost.png ├── stitched_filters_8x8.png └── synset_words.txt /01.mnist_mpl.py: -------------------------------------------------------------------------------- 1 | #coding=utf-8 2 | from keras.datasets import mnist 3 | from keras.models import Sequential 4 | from keras.layers.core import Dense, Dropout, Activation 5 | from keras.optimizers import SGD, Adam, RMSprop 6 | from keras.utils import np_utils 7 | from keras.utils.vis_utils import plot_model 8 | batch_size = 128 9 | nb_classes = 10 10 | nb_epoch = 20 11 | # 准备数据 12 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 13 | #将3D转化为2D 14 | X_train = X_train.reshape(60000, 784) 15 | X_test = X_test.reshape(10000, 784) 16 | X_train = X_train.astype('float32') 17 | X_test = X_test.astype('float32') 18 | # 对数据进行归一化到0-1 因为图像数据最大是255 19 | X_train /= 255 20 | X_test /= 255 21 | print(X_train.shape[0], 'train samples') 22 | print(X_test.shape[0], 'test samples') 23 | # 将类别向量(从0到nb_classes的整数向量)映射为二值类别矩阵 24 | Y_train = np_utils.to_categorical(y_train, nb_classes) 25 | Y_test = np_utils.to_categorical(y_test, nb_classes) 26 | print(Y_train.shape) 27 | print(Y_test.shape) 28 | # 建立模型 29 | model = Sequential() 30 | model.add(Dense(512, input_shape=(784,)))#Dense的第一个参数是输出维度 31 | model.add(Activation('relu')) 32 | model.add(Dropout(0.3)) 33 | model.add(Dense(512)) 34 | model.add(Activation('relu')) 35 | model.add(Dropout(0.3)) 36 | model.add(Dense(10)) 37 | model.add(Activation('softmax')) 38 | print(model.summary()) 39 | plot_model(model, to_file='model.png') 40 | # 训练和评估 41 | model.compile(loss='categorical_crossentropy', 42 | optimizer=RMSprop(), 43 | metrics=['accuracy']) 44 | history = model.fit(X_train,Y_train,batch_size=batch_size,epochs=nb_epoch,verbose=1,validation_data=(X_test,Y_test)) 45 | score = model.evaluate(X_test, Y_test, verbose=0) 46 | print('Test score:', score[0]) 47 | print('Test accuracy:', score[1]) 48 | #模型保存 49 | model.save('mnist-mpl.h5') 50 | -------------------------------------------------------------------------------- /02.load_model.py: -------------------------------------------------------------------------------- 1 | from keras.datasets import mnist 2 | from keras.models import Sequential 3 | from keras.layers.core import Dense, Dropout, Activation 4 | from keras.optimizers import SGD, Adam, RMSprop 5 | from keras.utils import np_utils 6 | from keras.models import load_model 7 | batch_size = 128 8 | nb_classes = 10 9 | nb_epoch = 20 10 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 11 | X_train = X_train.reshape(60000, 784) 12 | X_test = X_test.reshape(10000, 784) 13 | X_train = X_train.astype('float32') 14 | X_test = X_test.astype('float32') 15 | X_train /= 255 16 | X_test /= 255 17 | print(X_train.shape[0], 'train samples') 18 | print(X_test.shape[0], 'test samples') 19 | Y_train = np_utils.to_categorical(y_train, nb_classes) 20 | Y_test = np_utils.to_categorical(y_test, nb_classes) 21 | model = load_model('mnist-mpl.h5') 22 | model.summary() 23 | model.compile(loss='categorical_crossentropy', 24 | optimizer=RMSprop(), 25 | metrics=['accuracy']) 26 | score = model.evaluate(X_test, Y_test, verbose=0) 27 | print('Test score:', score[0]) 28 | print('Test accuracy:', score[1]) -------------------------------------------------------------------------------- /03.plot_acc_loss.py: -------------------------------------------------------------------------------- 1 | import keras 2 | from keras.datasets import mnist 3 | from keras.models import Sequential 4 | from keras.layers.core import Dense, Dropout, Activation 5 | from keras.optimizers import SGD, Adam, RMSprop 6 | from keras.utils import np_utils 7 | import matplotlib.pyplot as plt 8 | 9 | #写一个LossHistory类,保存loss和acc 10 | class LossHistory(keras.callbacks.Callback): 11 | def on_train_begin(self, logs={}): 12 | self.losses = {'batch': [], 'epoch': []} 13 | self.accuracy = {'batch': [], 'epoch': []} 14 | self.val_loss = {'batch': [], 'epoch': []} 15 | self.val_acc = {'batch': [], 'epoch': []} 16 | 17 | def on_batch_end(self, batch, logs={}): 18 | self.losses['batch'].append(logs.get('loss')) 19 | self.accuracy['batch'].append(logs.get('acc')) 20 | self.val_loss['batch'].append(logs.get('val_loss')) 21 | self.val_acc['batch'].append(logs.get('val_acc')) 22 | 23 | def on_epoch_end(self, batch, logs={}): 24 | self.losses['epoch'].append(logs.get('loss')) 25 | self.accuracy['epoch'].append(logs.get('acc')) 26 | self.val_loss['epoch'].append(logs.get('val_loss')) 27 | self.val_acc['epoch'].append(logs.get('val_acc')) 28 | 29 | def loss_plot(self, loss_type): 30 | iters = range(len(self.losses[loss_type])) 31 | #创建一个图 32 | plt.figure() 33 | # acc 34 | plt.plot(iters, self.accuracy[loss_type], 'r', label='train acc')#plt.plot(x,y),这个将数据画成曲线 35 | # loss 36 | plt.plot(iters, self.losses[loss_type], 'g', label='train loss') 37 | if loss_type == 'epoch': 38 | # val_acc 39 | plt.plot(iters, self.val_acc[loss_type], 'b', label='val acc') 40 | # val_loss 41 | plt.plot(iters, self.val_loss[loss_type], 'k', label='val loss') 42 | plt.grid(True)#设置网格形式 43 | plt.xlabel(loss_type) 44 | plt.ylabel('acc-loss')#给x,y轴加注释 45 | plt.legend(loc="upper right")#设置图例显示位置 46 | plt.show() 47 | batch_size = 128 48 | nb_classes = 10 49 | nb_epoch = 20 50 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 51 | X_train = X_train.reshape(60000, 784) 52 | X_test = X_test.reshape(10000, 784) 53 | X_train = X_train.astype('float32') 54 | X_test = X_test.astype('float32') 55 | X_train /= 255 56 | X_test /= 255 57 | print(X_train.shape[0], 'train samples') 58 | print(X_test.shape[0], 'test samples') 59 | Y_train = np_utils.to_categorical(y_train, nb_classes) 60 | Y_test = np_utils.to_categorical(y_test, nb_classes) 61 | 62 | model = Sequential() 63 | model.add(Dense(512, input_shape=(784,))) 64 | model.add(Activation('relu')) 65 | model.add(Dropout(0.2)) 66 | model.add(Dense(512)) 67 | model.add(Activation('relu')) 68 | model.add(Dropout(0.2)) 69 | model.add(Dense(10)) 70 | model.add(Activation('softmax')) 71 | 72 | model.compile(loss='categorical_crossentropy', 73 | optimizer=RMSprop(), 74 | metrics=['accuracy']) 75 | #创建一个实例LossHistory 76 | history = LossHistory() 77 | model.fit(X_train, Y_train, 78 | batch_size=batch_size, nb_epoch=nb_epoch, 79 | verbose=1, 80 | validation_data=(X_test, Y_test), 81 | callbacks=[history])#callbacks回调,将数据传给history 82 | #模型评估 83 | score = model.evaluate(X_test, Y_test, verbose=0) 84 | print('Test score:', score[0]) 85 | print('Test accuracy:', score[1]) 86 | #绘制acc-loss曲线 87 | history.loss_plot('epoch') -------------------------------------------------------------------------------- /04.mnist_cnn.py: -------------------------------------------------------------------------------- 1 | from keras.datasets import mnist 2 | from keras.models import Sequential 3 | from keras.layers import Dense, Dropout, Activation, Flatten 4 | from keras.layers import Convolution2D, MaxPooling2D 5 | from keras.utils import np_utils 6 | from keras import backend as K 7 | from keras.utils.vis_utils import plot_model 8 | batch_size = 128 9 | nb_classes = 10 10 | nb_epoch = 12 11 | 12 | # 输入数据的维度 13 | img_rows, img_cols = 28, 28 14 | # 使用的卷积滤波器的数量 15 | nb_filters = 32 16 | # 用于 max pooling 的池化面积 17 | pool_size = (2, 2) 18 | # 卷积核的尺寸 19 | kernel_size = (3, 3) 20 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 21 | #同样要reshape,只是现在图片是三维矩阵 22 | X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) 23 | X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) 24 | input_shape = (img_rows, img_cols, 1) 25 | X_train = X_train.astype('float32') 26 | X_test = X_test.astype('float32') 27 | X_train /= 255 28 | X_test /= 255 29 | print('X_train shape:', X_train.shape) 30 | print(X_train.shape[0], 'train samples') 31 | print(X_test.shape[0], 'test samples') 32 | Y_train = np_utils.to_categorical(y_train, nb_classes) 33 | Y_test = np_utils.to_categorical(y_test, nb_classes) 34 | model = Sequential() 35 | 36 | model.add(Convolution2D(nb_filters, kernel_size,strides=(1, 1), 37 | padding='valid', 38 | input_shape=input_shape))#用作第一层时,需要输入input_shape参数 39 | model.add(Activation('relu')) 40 | model.add(Convolution2D(nb_filters, kernel_size)) 41 | model.add(Activation('relu')) 42 | model.add(MaxPooling2D(pool_size=pool_size)) 43 | model.add(Dropout(0.25))#Dense()的前面要减少连接点,防止过拟合,故通常要Dropout层或池化层 44 | model.add(Flatten())#Dense()层的输入通常是2D张量,故应使用Flatten层或全局平均池化 45 | model.add(Dense(128)) 46 | model.add(Activation('relu'))#Dense( )层的后面通常要加非线性化函数 47 | model.add(Dropout(0.5)) 48 | model.add(Dense(nb_classes)) 49 | model.add(Activation('softmax'))#分类 50 | model.summary() 51 | plot_model(model, to_file='model-cnn.png') 52 | model.compile(loss='categorical_crossentropy', 53 | optimizer='adadelta', 54 | metrics=['accuracy']) 55 | model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, 56 | verbose=1, validation_data=(X_test, Y_test)) 57 | score = model.evaluate(X_test, Y_test, verbose=0) 58 | print('Test score:', score[0]) 59 | print('Test accuracy:', score[1]) -------------------------------------------------------------------------------- /05.cifar10_cnn.py: -------------------------------------------------------------------------------- 1 | from keras.datasets import cifar10 2 | from keras.preprocessing.image import ImageDataGenerator 3 | from keras.models import Sequential 4 | from keras.layers import Dense, Dropout, Activation, Flatten 5 | from keras.layers import Convolution2D, MaxPooling2D 6 | from keras.optimizers import SGD 7 | from keras.utils import np_utils 8 | batch_size = 32 9 | nb_classes = 10 10 | nb_epoch = 20 11 | kernel_size = (3, 3) 12 | data_augmentation = True 13 | # input image dimensions 14 | img_rows, img_cols = 32, 32 15 | # the CIFAR10 images are RGB 16 | img_channels = 3 17 | (X_train, y_train), (X_test, y_test) = cifar10.load_data() 18 | print('X_train shape:', X_train.shape) 19 | #可将数据归一化 20 | X_train = X_train.astype('float32') 21 | X_test = X_test.astype('float32') 22 | X_train /= 255 23 | X_test /= 255 24 | print(X_train.shape[0], 'train samples') 25 | print(X_test.shape[0], 'test samples') 26 | Y_train = np_utils.to_categorical(y_train, nb_classes) 27 | Y_test = np_utils.to_categorical(y_test, nb_classes) 28 | model = Sequential() 29 | model.add(Convolution2D(32, kernel_size, padding='same', 30 | input_shape=X_train.shape[1:])) 31 | model.add(Activation('relu')) 32 | model.add(Convolution2D(32, kernel_size)) 33 | model.add(Activation('relu')) 34 | model.add(MaxPooling2D(pool_size=(2, 2))) 35 | model.add(Dropout(0.25)) 36 | model.add(Convolution2D(64,kernel_size, padding='same')) 37 | model.add(Activation('relu')) 38 | model.add(Convolution2D(64, kernel_size)) 39 | model.add(Activation('relu')) 40 | model.add(MaxPooling2D(pool_size=(2, 2))) 41 | model.add(Dropout(0.25)) 42 | model.add(Flatten()) 43 | model.add(Dense(512)) 44 | model.add(Activation('relu')) 45 | model.add(Dropout(0.5)) 46 | model.add(Dense(nb_classes)) 47 | model.add(Activation('softmax')) 48 | model.summary() 49 | sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 50 | model.compile(loss='categorical_crossentropy', 51 | optimizer=sgd, 52 | metrics=['accuracy']) 53 | if not data_augmentation: 54 | print('Not using data augmentation.') 55 | model.fit(X_train, Y_train, 56 | batch_size=batch_size, 57 | nb_epoch=nb_epoch, 58 | validation_data=(X_test, Y_test), 59 | shuffle=True) 60 | else: 61 | print('Using real-time data augmentation.') 62 | 63 | # 这将做预处理和实时数据增加 64 | datagen = ImageDataGenerator( 65 | featurewise_center=False, # 在数据集上将输入平均值设置为0 66 | samplewise_center=False, # 将每个样本均值设置为0 67 | featurewise_std_normalization=False, # 将输入除以数据集的std 68 | samplewise_std_normalization=False, # 将每个输入除以其std 69 | zca_whitening=False, # 应用ZCA白化 70 | rotation_range=0, # 在一个范围下随机旋转图像(degrees, 0 to 180) 71 | width_shift_range=0.1, # 水平随机移位图像(总宽度的分数) 72 | height_shift_range=0.1, # 随机地垂直移动图像(总高度的分数) 73 | horizontal_flip=True, # 随机翻转图像 74 | vertical_flip=False) # 随机翻转图像 75 | 76 | # 计算特征方向归一化所需的数量 77 | # (std, mean, and principal components if ZCA whitening is applied) 78 | datagen.fit(X_train) 79 | 80 | # fit the model on the batches generated by datagen.flow() 81 | model.fit_generator(datagen.flow(X_train, Y_train, 82 | batch_size=batch_size), 83 | samples_per_epoch=X_train.shape[0], 84 | epochs=nb_epoch, 85 | validation_data=(X_test, Y_test)) -------------------------------------------------------------------------------- /06.mnist_lstm.py: -------------------------------------------------------------------------------- 1 | from keras.models import Sequential 2 | from keras.layers import LSTM, Dense 3 | from keras.datasets import mnist 4 | from keras.utils import np_utils 5 | from keras import initializers 6 | from keras.utils.vis_utils import plot_model 7 | 8 | # Hyper parameters 9 | batch_size = 128 10 | nb_epoch = 10 11 | 12 | # Parameters for MNIST dataset 13 | img_rows, img_cols = 28, 28 14 | nb_classes = 10 15 | 16 | # Parameters for LSTM network 17 | nb_lstm_outputs = 30 18 | nb_time_steps = img_rows 19 | dim_input_vector = img_cols 20 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 21 | print('X_train original shape:', X_train.shape) 22 | input_shape = (nb_time_steps, dim_input_vector) 23 | X_train = X_train.astype('float32') 24 | X_test = X_test.astype('float32') 25 | X_train /= 255 26 | X_test /= 255 27 | Y_train = np_utils.to_categorical(y_train, nb_classes) 28 | Y_test = np_utils.to_categorical(y_test, nb_classes) 29 | print('X_train shape:', X_train.shape) 30 | print(X_train.shape[0], 'train samples') 31 | print(X_test.shape[0], 'test samples') 32 | model = Sequential() 33 | #第一个参数是输出维度,第二个参数是输入图片的维度 34 | model.add(LSTM(nb_lstm_outputs, input_shape=input_shape)) 35 | model.add(Dense(nb_classes, activation='softmax', kernel_initializer=initializers.random_normal(stddev=0.01))) 36 | model.summary() 37 | plot_model(model, to_file='lstm_model.png') 38 | model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) 39 | history = model.fit(X_train, Y_train, epochs=nb_epoch, batch_size=batch_size, shuffle=True, verbose=1) 40 | score = model.evaluate(X_test, Y_test, verbose=1) 41 | print('Test score:', score[0]) 42 | print('Test accuracy:', score[1]) -------------------------------------------------------------------------------- /07.vgg-16.py: -------------------------------------------------------------------------------- 1 | from keras.applications.vgg16 import VGG16 2 | from keras.preprocessing import image 3 | from keras.applications.vgg16 import preprocess_input,decode_predictions 4 | from keras.models import Model 5 | import numpy as np 6 | import matplotlib.pyplot as plt 7 | 8 | '''加载并显示图片''' 9 | img_path = 'elephant.jpg' 10 | img = image.load_img(img_path, target_size=(224, 224)) 11 | plt.imshow(img) 12 | plt.show() 13 | 14 | '''图片预处理''' 15 | #将图片转成numpy array 16 | x = image.img_to_array(img) 17 | #扩展维度,因为preprocess_input需要4D的格式 18 | x = np.expand_dims(x, axis=0) 19 | #对张量进行预处理 20 | x = preprocess_input(x) 21 | 22 | #加载VGG16模型(包含全连接层) 23 | model = VGG16(include_top=True, weights='imagenet') 24 | preds = model.predict(x) 25 | # 将结果解码成一个元组列表(类、描述、概率)(批次中每个样本的一个这样的列表) 26 | print('Predicted:', decode_predictions(preds, top=3)[0]) 27 | -------------------------------------------------------------------------------- /08.conv_filter_visualization.py: -------------------------------------------------------------------------------- 1 | #说明:通过在输入空间的梯度上升,可视化VGG16的滤波器。 2 | from scipy.misc import imsave 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import time 6 | from keras.applications import vgg16 7 | from keras import backend as K 8 | img_width = 128 9 | img_height = 128 10 | layer_name = 'block5_conv1' 11 | 12 | #将张量转换成有效图像 13 | def deprocess_image(x): 14 | # 对张量进行规范化 15 | x -= x.mean() 16 | x /= (x.std() + 1e-5) 17 | x *= 0.1 18 | # clip to [0, 1] 19 | x += 0.5 20 | x = np.clip(x, 0, 1) 21 | # 转化到RGB数组 22 | x *= 255 23 | x = np.clip(x, 0, 255).astype('uint8') 24 | return x 25 | def normalize(x): 26 | # 效用函数通过其L2范数标准化张量 27 | return x / (K.sqrt(K.mean(K.square(x))) + 1e-5) 28 | 29 | model = vgg16.VGG16(weights='imagenet', include_top=False) 30 | print('Model loaded.') 31 | model.summary() 32 | 33 | input_img = model.input 34 | #用一个字典layer_dict存放Vgg16模型的每一层 35 | layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]]) 36 | print(layer_dict.keys()) 37 | print(layer_dict.values()) 38 | 39 | #提取滤波器 40 | #通过梯度上升,修改输入图像,使得滤波器的激活最大化。这是的输入图像就是我们要可视化的滤波器。 41 | kept_filters = [] 42 | for filter_index in range(0, 200): 43 | # 我们只扫描前200个滤波器, 44 | # 但实际上有512个 45 | print('Processing filter %d' % filter_index) 46 | start_time = time.time() 47 | # 我们构建一个损耗函数,使所考虑的层的第n个滤波器的激活最大化 48 | #由字典索引输出 49 | layer_output = layer_dict[layer_name].output 50 | loss = K.mean(layer_output[:, :, :, filter_index]) 51 | 52 | # 我们计算输入图像的梯度与这个损失 53 | grads = K.gradients(loss, input_img)[0] 54 | 55 | # 归一化技巧:我们规范化梯度 56 | grads = normalize(grads) 57 | 58 | # 此函数返回给定输入图像的损耗和梯度 59 | # inputs: List of placeholder tensors. 60 | # outputs: List of output tensors. 61 | iterate = K.function([input_img], [loss, grads]) 62 | 63 | # 梯度上升的步长 64 | step = 1. 65 | 66 | # 我们从带有一些随机噪声的灰色图像开始 67 | input_img_data = np.random.random((1, img_width, img_height, 3)) 68 | input_img_data = (input_img_data - 0.5) * 20 + 128 69 | 70 | # 我们运行梯度上升20步 71 | for i in range(20): 72 | loss_value, grads_value = iterate([input_img_data]) 73 | #每次上升一步,逐次上升 74 | input_img_data += grads_value * step 75 | 76 | print('Current loss value:', loss_value) 77 | if loss_value <= 0.: 78 | # 一些过滤器陷入0,我们可以跳过它们 79 | break 80 | 81 | # 解码所得到的输入图像 82 | if loss_value > 0: 83 | img = deprocess_image(input_img_data[0]) 84 | #将解码后的图像和损耗值加入列表 85 | kept_filters.append((img, loss_value)) 86 | end_time = time.time() 87 | print('Filter %d processed in %ds' % (filter_index, end_time - start_time)) 88 | 89 | 90 | # 我们将在8 x 8网格上选择最好的64个滤波器。 91 | n = 8 92 | # 具有最高损失的过滤器被假定为更好看。 93 | # 我们将只保留前64个过滤器。 94 | #Lambda:本函数用以对上一层的输出施以任何Theano/TensorFlow表达式 95 | kept_filters.sort(key=lambda x: x[1], reverse=True) 96 | kept_filters = kept_filters[:n * n] 97 | 98 | # 构建一张黑色的图片,有足够的空间 99 | # 我们的尺寸为128 x 128的8 x 8过滤器,中间有5px的边距 100 | margin = 5 101 | width = n * img_width + (n - 1) * margin 102 | height = n * img_height + (n - 1) * margin 103 | stitched_filters = np.zeros((width, height, 3)) 104 | 105 | # 使用我们保存的过滤器填充图片 106 | for i in range(n): 107 | for j in range(n): 108 | img, loss = kept_filters[i * n + j] 109 | stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width, 110 | (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img 111 | 112 | #显示滤波器 113 | plt.imshow(img) 114 | plt.show() 115 | plt.imshow(stitched_filters) 116 | plt.show() 117 | # 保存结果,将数组保存为图像 118 | imsave('stitched_filters_%dx%d.png' % (n, n), stitched_filters) -------------------------------------------------------------------------------- /09.variational_autoencoder.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from keras.layers import Input, Dense, Lambda 4 | from keras.models import Model 5 | from keras import backend as K 6 | from keras import objectives 7 | from keras.datasets import mnist 8 | batch_size = 100 9 | original_dim = 784 10 | latent_dim = 2 11 | intermediate_dim = 256 12 | nb_epoch = 50 13 | epsilon_std = 1.0 14 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 15 | x_train = x_train.astype('float32') 16 | x_test = x_test.astype('float32') 17 | x_train/=255 18 | x_test/=255 19 | #np.prob返回数组元素在给定轴上的乘积。reshape将3D转化成2D 20 | x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) 21 | x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) 22 | print(x_train.shape[0], 'train samples') 23 | print(x_test.shape[0], 'test samples') 24 | def sampling(args): 25 | z_mean, z_log_var = args 26 | epsilon = K.random_normal(shape=(batch_size, latent_dim),mean=0.0, stddev=1.0, dtype=None, seed=None) 27 | return z_mean + K.exp(z_log_var / 2) * epsilon 28 | def vae_loss(x, x_decoded_mean): 29 | xent_loss = original_dim * objectives.binary_crossentropy(x, x_decoded_mean) 30 | kl_loss = - 0.5 * K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1) 31 | return xent_loss + kl_loss 32 | 33 | 34 | # 建立模型 35 | #Input用于实例化一个张量,batch_shape相当于多个input_shape 36 | x = Input(batch_shape=(batch_size, original_dim)) 37 | h = Dense(intermediate_dim, activation='relu')(x) 38 | z_mean = Dense(latent_dim)(h) 39 | encoder = Model(x, z_mean)#编码器 40 | 41 | z_log_var = Dense(latent_dim)(h) 42 | z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])#Lambda是匿名函数 43 | decoder_h = Dense(intermediate_dim, activation='relu') 44 | decoder_mean = Dense(original_dim, activation='sigmoid') 45 | h_decoded = decoder_h(z) 46 | x_decoded_mean = decoder_mean(h_decoded) 47 | #可以用Model直接建模型,也可以逐个model.add() 48 | vae = Model(x, x_decoded_mean)#解码器 49 | vae.compile(optimizer='rmsprop', loss=vae_loss) 50 | vae.fit(x_train, x_train, 51 | shuffle=True, 52 | epochs=nb_epoch, 53 | batch_size=batch_size, 54 | validation_data=(x_test, x_test)) 55 | 56 | # 显示一个数字类的二维图。 57 | x_test_encoded = encoder.predict(x_test, batch_size=batch_size) 58 | #figsize是图像的长和宽 59 | plt.figure(figsize=(6, 6)) 60 | # 散点图,前两个参数是数据,c参数是散点颜色 61 | plt.scatter(x_test_encoded[:, 0], x_test_encoded[:, 1], c=y_test) 62 | #颜色渐变条 63 | plt.colorbar() 64 | plt.show() 65 | 66 | # 构建一个可以从学习分布中采样的数字生成器 67 | decoder_input = Input(shape=(latent_dim,)) 68 | _h_decoded = decoder_h(decoder_input) 69 | _x_decoded_mean = decoder_mean(_h_decoded) 70 | generator = Model(decoder_input, _x_decoded_mean) 71 | 72 | # 显示数字的二维流形 73 | n = 15 # 用15x15数字图 74 | digit_size = 28 75 | figure = np.zeros((digit_size * n, digit_size * n)) 76 | # 我们将在[-15, 15 ]标准偏差( 等距离)中采样n个点。 77 | grid_x = np.linspace(-15, 15, n) 78 | grid_y = np.linspace(-15, 15, n) 79 | #enumerate用于枚举 80 | for i, yi in enumerate(grid_x): 81 | for j, xi in enumerate(grid_y): 82 | z_sample = np.array([[xi, yi]]) 83 | x_decoded = generator.predict(z_sample) 84 | digit = x_decoded[0].reshape(digit_size, digit_size) 85 | figure[i * digit_size: (i + 1) * digit_size, 86 | j * digit_size: (j + 1) * digit_size] = digit 87 | 88 | plt.figure(figsize=(10, 10)) 89 | plt.imshow(figure) 90 | plt.show() -------------------------------------------------------------------------------- /10.mnist_transfer_cnn.py: -------------------------------------------------------------------------------- 1 | #说明:该程序演示将一个预训练好的模型在新数据集上重新fine-tuning的过程。我们冻结卷积层,只调整全连接层。 2 | #在MNIST数据集上使用前五个数字[0...4]训练一个卷积网络。 3 | #在后五个数字[5...9]用卷积网络做分类,冻结卷积层并且微调全连接层 4 | import datetime 5 | from keras.datasets import mnist 6 | from keras.models import Sequential 7 | from keras.layers import Dense, Dropout, Activation, Flatten 8 | from keras.layers import Convolution2D, MaxPooling2D 9 | from keras.utils import np_utils 10 | from keras import backend as K 11 | now = datetime.datetime.now 12 | 13 | batch_size = 128 14 | nb_classes = 5 15 | nb_epoch = 5 16 | 17 | # 输入图像的维度 18 | img_rows, img_cols = 28, 28 19 | # 使用卷积滤波器的数量 20 | nb_filters = 32 21 | # 用于max pooling的pooling面积的大小 22 | pool_size = 2 23 | # 卷积核的尺度 24 | kernel_size = (3,3) 25 | input_shape = (img_rows, img_cols, 1) 26 | 27 | # 数据,在训练和测试数据集上混洗和拆分 28 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 29 | X_train_lt5 = X_train[y_train < 5] 30 | y_train_lt5 = y_train[y_train < 5] 31 | X_test_lt5 = X_test[y_test < 5] 32 | y_test_lt5 = y_test[y_test < 5] 33 | 34 | X_train_gte5 = X_train[y_train >= 5] 35 | #使标签从0~4,故-5 36 | y_train_gte5 = y_train[y_train >= 5] - 5 37 | X_test_gte5 = X_test[y_test >= 5] 38 | y_test_gte5 = y_test[y_test >= 5] - 5 39 | 40 | # 模型的训练函数 41 | def train_model(model, train, test, nb_classes): 42 | #train[0]是图片,train[1]是标签 43 | X_train = train[0].reshape((train[0].shape[0],) + input_shape)#1D+3D=4D 44 | X_test = test[0].reshape((test[0].shape[0],) + input_shape) 45 | X_train = X_train.astype('float32') 46 | X_test = X_test.astype('float32') 47 | X_train /= 255 48 | X_test /= 255 49 | print('X_train shape:', X_train.shape) 50 | print(X_train.shape[0], 'train samples') 51 | print(X_test.shape[0], 'test samples') 52 | Y_train = np_utils.to_categorical(train[1], nb_classes) 53 | Y_test = np_utils.to_categorical(test[1], nb_classes) 54 | model.compile(loss='categorical_crossentropy', 55 | optimizer='adadelta', 56 | metrics=['accuracy']) 57 | t = now() 58 | model.fit(X_train, Y_train, 59 | batch_size=batch_size, nb_epoch=nb_epoch, 60 | verbose=1, 61 | validation_data=(X_test, Y_test)) 62 | print('Training time: %s' % (now() - t)) 63 | score = model.evaluate(X_test, Y_test, verbose=0) 64 | print('Test score:', score[0]) 65 | print('Test accuracy:', score[1]) 66 | 67 | # 建立模型,构建卷积层(特征层)和全连接层(分类层) 68 | feature_layers = [ 69 | Convolution2D(nb_filters, kernel_size, 70 | padding='valid', 71 | input_shape=input_shape), 72 | Activation('relu'), 73 | Convolution2D(nb_filters, kernel_size), 74 | Activation('relu'), 75 | MaxPooling2D(pool_size=(pool_size, pool_size)), 76 | Dropout(0.25), 77 | Flatten(), 78 | ] 79 | classification_layers = [ 80 | Dense(128), 81 | Activation('relu'), 82 | Dropout(0.5), 83 | Dense(nb_classes), 84 | Activation('softmax') 85 | ] 86 | model = Sequential(feature_layers + classification_layers) 87 | 88 | #训练预训练模型用于5个数字[0...4]的分类任务 89 | train_model(model, 90 | (X_train_lt5, y_train_lt5), 91 | (X_test_lt5, y_test_lt5), nb_classes) 92 | 93 | #冻结特征层(用的是同一个模型,只是冻结了部分层) 94 | for l in feature_layers: 95 | l.trainable = False 96 | 97 | #fine-tuning分类层 98 | train_model(model, 99 | (X_train_gte5, y_train_gte5), 100 | (X_test_gte5, y_test_gte5), nb_classes) -------------------------------------------------------------------------------- /11.Keras_with_tensorflow.py: -------------------------------------------------------------------------------- 1 | #将keras作为tensorflow的精简接口 2 | from keras.datasets import mnist 3 | from keras.models import Sequential 4 | from keras.layers.core import Dense, Dropout, Activation 5 | from keras.optimizers import SGD, Adam, RMSprop 6 | from keras.utils import np_utils 7 | from keras.objectives import categorical_crossentropy 8 | import tensorflow as tf 9 | from keras import backend as K 10 | batch_size = 128 11 | nb_classes = 10 12 | nb_epoch = 20 13 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 14 | X_train = X_train.reshape(60000, 784) 15 | X_test = X_test.reshape(10000, 784) 16 | X_train = X_train.astype('float32') 17 | X_test = X_test.astype('float32') 18 | X_train /= 255 19 | X_test /= 255 20 | print(X_train.shape[0], 'train samples') 21 | print(X_test.shape[0], 'test samples') 22 | Y_train = np_utils.to_categorical(y_train, nb_classes) 23 | Y_test = np_utils.to_categorical(y_test, nb_classes) 24 | model = Sequential() 25 | model.add(Dense(512, input_shape=(784,))) 26 | model.add(Activation('relu')) 27 | model.add(Dropout(0.2)) 28 | model.add(Dense(512)) 29 | model.add(Activation('relu')) 30 | model.add(Dropout(0.2)) 31 | model.add(Dense(10)) 32 | model.add(Activation('softmax')) 33 | model.summary() 34 | model.compile(loss='categorical_crossentropy', 35 | optimizer=RMSprop(), 36 | metrics=['accuracy']) 37 | history = model.fit(X_train, Y_train, 38 | batch_size=batch_size, nb_epoch=nb_epoch, 39 | verbose=1, validation_data=(X_test, Y_test)) 40 | score = model.evaluate(X_test, Y_test, verbose=0) 41 | print('Test score:', score[0]) 42 | print('Test accuracy:', score[1]) -------------------------------------------------------------------------------- /12.mnist_sklearn_wrapper.py: -------------------------------------------------------------------------------- 1 | # 使用sklearn wrapper做参数搜索 2 | from keras.datasets import mnist 3 | from keras.models import Sequential 4 | from keras.layers import Dense, Dropout, Activation, Flatten 5 | from keras.layers import Convolution2D, MaxPooling2D 6 | from keras.utils import np_utils 7 | from keras.wrappers.scikit_learn import KerasClassifier 8 | from sklearn.grid_search import GridSearchCV 9 | nb_classes = 10 10 | img_rows, img_cols = 28, 28 11 | (X_train, y_train), (X_test, y_test) = mnist.load_data() 12 | X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) 13 | X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) 14 | X_train = X_train.astype('float32') 15 | X_test = X_test.astype('float32') 16 | X_train /= 255 17 | X_test /= 255 18 | y_train = np_utils.to_categorical(y_train, nb_classes) 19 | y_test = np_utils.to_categorical(y_test, nb_classes) 20 | def make_model(dense_layer_sizes, nb_filters, nb_conv, nb_pool): 21 | '''Creates model comprised of 2 convolutional layers followed by dense layers 22 | dense_layer_sizes: List of layer sizes. This list has one number for each layer 23 | nb_filters: Number of convolutional filters in each convolutional layer 24 | nb_conv: Convolutional kernel size 25 | nb_pool: Size of pooling area for max pooling 26 | ''' 27 | model = Sequential() 28 | model.add(Convolution2D(nb_filters, nb_conv, nb_conv, 29 | border_mode='valid', 30 | input_shape=(img_rows, img_cols, 1))) 31 | model.add(Activation('relu')) 32 | model.add(Convolution2D(nb_filters, nb_conv, nb_conv)) 33 | model.add(Activation('relu')) 34 | model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool))) 35 | model.add(Dropout(0.25)) 36 | 37 | model.add(Flatten()) 38 | for layer_size in dense_layer_sizes: 39 | model.add(Dense(layer_size))#加入多个全连接层 40 | model.add(Activation('relu')) 41 | model.add(Dropout(0.5)) 42 | model.add(Dense(nb_classes)) 43 | model.add(Activation('softmax')) 44 | 45 | model.compile(loss='categorical_crossentropy', 46 | optimizer='adadelta', 47 | metrics=['accuracy']) 48 | 49 | return model#对模型进行编译,但还没有训练 50 | 51 | #[32]是一个输出维度为32的Dense,[32,32]是两个输出维度为32的Dense 52 | dense_size_candidates = [[32], [64], [32, 32], [64, 64]] 53 | #使用sklearn的分类器接口:第一个参数是可调用的函数或类对象,第二个参数是模型参数和训练参数 54 | my_classifier = KerasClassifier(make_model, batch_size=32) 55 | #sklearn中的GridSearchCV函数 56 | #说明:对估计器的指定参数值进行穷举搜索。 57 | validator = GridSearchCV(my_classifier, 58 | param_grid={'dense_layer_sizes': dense_size_candidates, 59 | # nb_epoch可用于调整,即使不是模型构建函数的参数 60 | 'nb_epoch': [3, 6], 61 | 'nb_filters': [8], 62 | 'nb_conv': [3], 63 | 'nb_pool': [2]}, 64 | scoring='log_loss', 65 | n_jobs=1) 66 | validator.fit(X_train, y_train) 67 | #打印最好模型的参数 68 | print('The parameters of the best model are: ') 69 | print(validator.best_params_) 70 | #返回模型 71 | best_model = validator.best_estimator_.model 72 | metric_names = best_model.metrics_names 73 | metric_values = best_model.evaluate(X_test, y_test) 74 | print('\n') 75 | #返回名称和数值 76 | for metric, value in zip(metric_names, metric_values): 77 | print(metric, ': ', value) 78 | -------------------------------------------------------------------------------- /13.finetune_InceptionV3.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import glob 4 | from keras.applications.inception_v3 import InceptionV3, preprocess_input 5 | 6 | from keras.models import Model 7 | from keras.layers import Dense, GlobalAveragePooling2D 8 | from keras.preprocessing.image import ImageDataGenerator 9 | from keras.optimizers import SGD 10 | 11 | '''获取文件的个数''' 12 | 13 | def get_nb_files(directory): 14 | if not os.path.exists(directory): 15 | return 0 16 | cnt = 0 17 | for r, dirs, files in os.walk(directory): 18 | for dr in dirs: 19 | cnt += len(glob.glob(os.path.join(r, dr + "/*"))) 20 | return cnt 21 | 22 | 23 | IM_WIDTH, IM_HEIGHT = 299, 299 # InceptionV3指定的图片尺寸 24 | FC_SIZE = 1024 # 全连接层的节点个数 25 | NB_IV3_LAYERS_TO_FREEZE = 172 # 冻结层的数量 26 | 27 | train_dir = 'E:/data/flower_photos/train_split' # 训练集数据 28 | val_dir = 'E:/data/flower_photos/val_split' # 验证集数据 29 | nb_epoch = 3 30 | batch_size = 16 31 | nb_train_samples = get_nb_files(train_dir) # 训练样本个数 32 | nb_classes = len(glob.glob(train_dir + "/*")) # 分类数 33 | nb_val_samples = get_nb_files(val_dir) # 验证集样本个数 34 | 35 | 36 | def image_preprocess(): 37 | # 图片生成器 38 | #  训练集的图片生成器,通过参数的设置进行数据扩增 39 | train_datagen = ImageDataGenerator( 40 | preprocessing_function=preprocess_input, 41 | rotation_range=30, 42 | width_shift_range=0.2, 43 | height_shift_range=0.2, 44 | shear_range=0.2, 45 | zoom_range=0.2, 46 | horizontal_flip=True 47 | ) 48 | # 验证集的图片生成器,不进行数据扩增,只进行数据预处理 49 | val_datagen = ImageDataGenerator( 50 | preprocessing_function=preprocess_input, 51 | ) 52 | # 训练数据与测试数据 53 | train_generator = train_datagen.flow_from_directory( 54 | train_dir, 55 | target_size=(IM_WIDTH, IM_HEIGHT), 56 | batch_size=batch_size, class_mode='categorical') 57 | 58 | validation_generator = val_datagen.flow_from_directory( 59 | val_dir, 60 | target_size=(IM_WIDTH, IM_HEIGHT), 61 | batch_size=batch_size, class_mode='categorical') 62 | return train_generator, validation_generator 63 | 64 | 65 | # 添加顶层分类器 66 | def add_new_last_layer(base_model, nb_classes): 67 | x = base_model.output 68 | x = GlobalAveragePooling2D()(x) 69 | x = Dense(FC_SIZE, activation='relu')(x) 70 | predictions = Dense(nb_classes, activation='softmax')(x) 71 | model = Model(input=base_model.input, output=predictions) 72 | return model 73 | 74 | 75 | # 冻结base_model所有层,这样就可以正确获得bottleneck特征 76 | def setup_to_transfer_learn(model, base_model): 77 | for layer in base_model.layers: 78 | layer.trainable = False 79 | model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) 80 | 81 | 82 | # 冻结部分层,对顶层分类器进行Fine-tune 83 | def setup_to_finetune(model): 84 | for layer in model.layers[:NB_IV3_LAYERS_TO_FREEZE]: 85 | layer.trainable = False 86 | for layer in model.layers[NB_IV3_LAYERS_TO_FREEZE:]: 87 | layer.trainable = True 88 | model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) 89 | 90 | 91 | if __name__ == "__main__": 92 | 93 | '''图片预处理(包括数据扩增)''' 94 | train_generator,validation_generator=image_preprocess() 95 | 96 | '''加载base_model''' 97 | # 使用带有预训练权重的InceptionV3模型,但不包括顶层分类器 98 | base_model = InceptionV3(weights='imagenet', include_top=False) # 预先要下载no_top模型 99 | 100 | '''添加顶层分类器''' 101 | model = add_new_last_layer(base_model, nb_classes) # 从基本no_top模型上添加新层 102 | 103 | '''训练顶层分类器''' 104 | setup_to_transfer_learn(model, base_model) 105 | history_tl = model.fit_generator( 106 | train_generator, 107 | epochs=nb_epoch, 108 | steps_per_epoch=nb_train_samples // batch_size, 109 | validation_data=validation_generator, 110 | validation_steps=nb_val_samples // batch_size, 111 | class_weight='auto') 112 | 113 | '''对顶层分类器进行Fine-tune''' 114 | # Fine-tune以一个预训练好的网络为基础,在新的数据集上重新训练一小部分权重。fine-tune应该在很低的学习率下进行,通常使用SGD优化 115 | setup_to_finetune(model) 116 | history_ft = model.fit_generator( 117 | train_generator, 118 | steps_per_epoch=nb_train_samples // batch_size, 119 | epochs=nb_epoch, 120 | validation_data=validation_generator, 121 | validation_steps=nb_val_samples // batch_size, 122 | class_weight='auto') 123 | -------------------------------------------------------------------------------- /14.sigle_autoencoder.py: -------------------------------------------------------------------------------- 1 | from keras.layers import Input, Dense 2 | from keras.models import Model 3 | from keras.datasets import mnist 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | (x_train, _), (x_test, _) = mnist.load_data() 7 | x_train = x_train.astype('float32') 8 | x_test = x_test.astype('float32') 9 | x_train/=255 10 | x_test/=255 11 | x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) 12 | x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) 13 | print(x_train.shape) 14 | print(x_test.shape) 15 | 16 | # 构造单隐藏层的自编码器( 784转32再转784) 17 | 18 | encoding_dim = 32 # 32 floats -> compression of factor 24.5, assuming the input is 784 floats 19 | # this is our input placeholder 20 | input_img = Input(shape=(784,)) 21 | # "encoded" is the encoded representation of the input 22 | encoded = Dense(encoding_dim, activation='relu')(input_img) 23 | # "decoded" is the lossy reconstruction of the input 24 | decoded = Dense(784, activation='sigmoid')(encoded) 25 | # this model maps an input to its reconstruction 26 | autoencoder = Model(input=input_img, output=decoded) 27 | 28 | # 定义编码器(784转32) 29 | # this model maps an input to its encoded representation 30 | encoder = Model(input=input_img, output=encoded) 31 | 32 | # 定义解码器(32转784) 33 | # create a placeholder for an encoded (32-dimensional) input 34 | encoded_input = Input(shape=(encoding_dim,)) 35 | # retrieve the last layer of the autoencoder model 36 | decoder_layer = Dense(784, activation='sigmoid') 37 | # create the decoder model 38 | decoder = Model(input=encoded_input, output=decoder_layer(encoded_input)) 39 | 40 | # 编译和训练自编码器 41 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 42 | autoencoder.fit(x_train, x_train, 43 | nb_epoch=50, 44 | batch_size=256, 45 | shuffle=True, 46 | validation_data=(x_test, x_test)) 47 | 48 | # 在测试集上进行解码 49 | # encode and decode some digits 50 | # note that we take them from the *test* set 51 | encoded_imgs = encoder.predict(x_test) 52 | decoded_imgs = decoder.predict(encoded_imgs) 53 | 54 | # 显示结果 55 | n = 10 # how many digits we will display 56 | plt.figure(figsize=(20, 4)) 57 | for i in range(n): 58 | # display original 59 | # 分成2*n个子图,占用第i+1个子图 60 | ax = plt.subplot(2, n, i + 1) 61 | plt.imshow(x_test[i].reshape(28, 28)) 62 | plt.gray() 63 | # x轴,y轴不可见 64 | ax.get_xaxis().set_visible(False) 65 | ax.get_yaxis().set_visible(False) 66 | 67 | # display reconstruction 68 | # 分成2*n个子图,占用第i+1+n个子图 69 | ax = plt.subplot(2, n, i + 1 + n) 70 | plt.imshow(decoded_imgs[i].reshape(28, 28)) 71 | plt.gray() 72 | ax.get_xaxis().set_visible(False) 73 | ax.get_yaxis().set_visible(False) 74 | plt.show() 75 | -------------------------------------------------------------------------------- /15.multi_autoencoder.py: -------------------------------------------------------------------------------- 1 | from keras.layers import Input, Dense 2 | from keras.models import Model 3 | from keras.datasets import mnist 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | from keras.utils import np_utils 7 | (x_train, y_train), (x_test, y_test) = mnist.load_data() 8 | x_train = x_train.astype('float32') 9 | x_test = x_test.astype('float32') 10 | x_train /= 255 11 | x_test /= 255 12 | Y_train = np_utils.to_categorical(y_train, 10) 13 | Y_test = np_utils.to_categorical(y_test, 10) 14 | print(x_train.shape) 15 | print(x_test.shape) 16 | print('\n') 17 | print(y_train.shape) 18 | print(y_test.shape) 19 | x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:]))) 20 | x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:]))) 21 | # 相当于之前的X_train = X_train.reshape(60000, 784),X_test = X_test.reshape(10000, 784) 22 | print(x_train.shape) 23 | print(x_test.shape) 24 | # 构造多隐藏层的自编码器 25 | input_img = Input(shape=(784,)) 26 | encoded = Dense(128, activation='relu')(input_img) 27 | encoded = Dense(64, activation='relu')(encoded) 28 | encoded = Dense(32, activation='relu')(encoded) 29 | envoded=Model(input=input_img,outputs=encoded) 30 | decoded = Dense(64, activation='relu')(encoded) 31 | decoded = Dense(128, activation='relu')(decoded) 32 | decoded = Dense(784, activation='sigmoid')(decoded) 33 | # 最后Dense是还原成784维度 34 | autoencoder = Model(input=input_img, output=decoded) 35 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 36 | autoencoder.fit(x_train,x_train, 37 | nb_epoch=50, 38 | batch_size=256, 39 | shuffle=True, 40 | validation_data=(x_test,x_test)) 41 | 42 | 43 | autoencoder_imgs = autoencoder.predict(x_test) 44 | n = 10 # how many digits we will display 45 | plt.figure(figsize=(20, 4)) 46 | for i in range(n): 47 | # display original 48 | ax = plt.subplot(2, n, i + 1) 49 | plt.imshow(x_test[i].reshape(28, 28)) 50 | plt.gray() 51 | ax.get_xaxis().set_visible(False) 52 | ax.get_yaxis().set_visible(False) 53 | 54 | # display reconstruction 55 | ax = plt.subplot(2, n, i + 1 + n) 56 | plt.imshow(autoencoder_imgs[i].reshape(28, 28)) 57 | plt.gray() 58 | ax.get_xaxis().set_visible(False) 59 | ax.get_yaxis().set_visible(False) 60 | plt.show() -------------------------------------------------------------------------------- /16.Convolutional_autoencoder.py: -------------------------------------------------------------------------------- 1 | from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D 2 | from keras.models import Model 3 | from keras.datasets import mnist 4 | import numpy as np 5 | import matplotlib.pyplot as plt 6 | (x_train, _), (x_test, _) = mnist.load_data() 7 | x_train = x_train.astype('float32') 8 | x_test = x_test.astype('float32') 9 | x_train/=255 10 | x_test/=255 11 | x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) 12 | x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) 13 | input_img = Input(shape=(28, 28, 1)) 14 | #卷积层要输入三维卷积 15 | x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img) 16 | x = MaxPooling2D((2, 2), border_mode='same')(x) 17 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 18 | x = MaxPooling2D((2, 2), border_mode='same')(x) 19 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 20 | encoded = MaxPooling2D((2, 2), border_mode='same')(x) 21 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded) 22 | x = UpSampling2D((2, 2))(x) 23 | x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 24 | x = UpSampling2D((2, 2))(x) 25 | x = Convolution2D(16, 3, 3, activation='relu')(x) 26 | x = UpSampling2D((2, 2))(x) 27 | decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x) 28 | autoencoder = Model(input_img, decoded) 29 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 30 | autoencoder.fit(x_train, x_train, 31 | nb_epoch=50, 32 | batch_size=128, 33 | shuffle=True, 34 | validation_data=(x_test, x_test)) 35 | decoded_imgs = autoencoder.predict(x_test) 36 | n = 10 37 | plt.figure(figsize=(20, 4)) 38 | for i in range(n): 39 | # display original 40 | ax = plt.subplot(2, n, i+1) 41 | plt.imshow(x_test[i].reshape(28, 28)) 42 | plt.gray() 43 | ax.get_xaxis().set_visible(False) 44 | ax.get_yaxis().set_visible(False) 45 | 46 | # display reconstruction 47 | ax = plt.subplot(2, n, i + n+1) 48 | plt.imshow(decoded_imgs[i].reshape(28, 28)) 49 | plt.gray() 50 | ax.get_xaxis().set_visible(False) 51 | ax.get_yaxis().set_visible(False) 52 | plt.show() 53 | autoencoder.summary() 54 | 55 | # 中间编码层可视化 56 | model_extractfeatures = Model(inputs=autoencoder.input, outputs=autoencoder.get_layer('max_pooling2d_3').output) 57 | encoded_imgs = model_extractfeatures.predict(x_test) 58 | print(encoded_imgs.shape) 59 | n = 10 60 | plt.figure(figsize=(20, 8)) 61 | for i in range(n): 62 | ax = plt.subplot(1, n, i+1) 63 | plt.imshow(encoded_imgs[i].reshape(4, 4 * 8).T) 64 | plt.gray() 65 | ax.get_xaxis().set_visible(False) 66 | ax.get_yaxis().set_visible(False) 67 | plt.show() -------------------------------------------------------------------------------- /17.Image_denoising.py: -------------------------------------------------------------------------------- 1 | from keras.datasets import mnist 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D 5 | from keras.models import Model 6 | (x_train, _), (x_test, _) = mnist.load_data() 7 | x_train = x_train.astype('float32') 8 | x_test = x_test.astype('float32') 9 | x_train/=255. 10 | x_test/=255. 11 | x_train = np.reshape(x_train, (len(x_train), 28, 28, 1)) # adapt this if using `channels_first` image data format 12 | x_test = np.reshape(x_test, (len(x_test), 28, 28, 1)) # adapt this if using `channels_first` image data format 13 | noise_factor = 0.5 14 | x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape) 15 | x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape) 16 | # 对数据进行裁剪,使其范围为0.-- 1. 17 | x_train_noisy = np.clip(x_train_noisy, 0., 1.) 18 | x_test_noisy = np.clip(x_test_noisy, 0., 1.) 19 | input_img = Input(shape=(28, 28, 1)) # adapt this if using `channels_first` image data format 20 | x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img) 21 | x = MaxPooling2D((2, 2), padding='same')(x) 22 | x = Conv2D(32, (3, 3), activation='relu', padding='same')(x) 23 | encoded = MaxPooling2D((2, 2), padding='same')(x) 24 | # at this point the representation is (7, 7, 32) 25 | x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded) 26 | # 将数据的行和列分别重复size[0]和size[1]次 27 | x = UpSampling2D((2, 2))(x) 28 | x = Conv2D(32, (3, 3), activation='relu', padding='same')(x) 29 | x = UpSampling2D((2, 2))(x) 30 | decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x) 31 | autoencoder = Model(input_img, decoded) 32 | autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 33 | autoencoder.fit(x_train_noisy, x_train, 34 | epochs=30, 35 | batch_size=128, 36 | shuffle=True, 37 | validation_data=(x_test_noisy, x_test), 38 | ) 39 | decoded_imgs = autoencoder.predict(x_test_noisy) 40 | n = 10 41 | plt.figure(figsize=(20, 2)) 42 | for i in range(n): 43 | ax = plt.subplot(2, n, i+1) 44 | plt.imshow(x_test_noisy[i].reshape(28, 28)) 45 | plt.gray() 46 | ax.get_xaxis().set_visible(False) 47 | ax.get_yaxis().set_visible(False) 48 | ax = plt.subplot(2, n, i + n + 1) 49 | plt.imshow(decoded_imgs[i].reshape(28, 28)) 50 | plt.gray() 51 | ax.get_xaxis().set_visible(False) 52 | ax.get_yaxis().set_visible(False) 53 | plt.show() 54 | autoencoder.summary() -------------------------------------------------------------------------------- /18.GloVe_word_embeddings.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import numpy as np 4 | from keras.utils import to_categorical 5 | from keras.layers import Embedding 6 | from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D, Flatten,MaxPooling1D,Conv1D 7 | from keras.models import Model 8 | texts = [] # list of text samples 9 | labels_index = {} # 由标签名称映射到ID的字典 10 | labels = [] # list of label ids 11 | TEXT_DATA_DIR='/news20/20_newsgroup' 12 | GLOVE_DIR='/glove.6B' 13 | MAX_NB_WORDS=20000 14 | MAX_SEQUENCE_LENGTH=1000 15 | VALIDATION_SPLIT=0.2 16 | EMBEDDING_DIM=100 17 | 18 | # 遍历下语料文件下的所有文件夹,获得不同类别的新闻以及对应的类别标签 19 | for name in sorted(os.listdir(TEXT_DATA_DIR)): 20 | # 文件夹名称即为类别标签 21 | path = os.path.join(TEXT_DATA_DIR, name) 22 | if os.path.isdir(path): 23 | label_id = len(labels_index)# 随着循环,id由0递增 24 | labels_index[name] = label_id 25 | for fname in sorted(os.listdir(path)): 26 | if fname.isdigit(): 27 | fpath = os.path.join(path, fname) 28 | if sys.version_info < (3,): 29 | f = open(fpath) 30 | else: 31 | f = open(fpath, encoding='latin-1') 32 | # 打开新闻样本 33 | t = f.read() 34 | i = t.find('\n\n') 35 | if 0 < i: 36 | t = t[i:] 37 | texts.append(t) 38 | # 保存新闻样本的列表 39 | f.close() 40 | labels.append(label_id) 41 | # 保存新闻样本对应的类别ID的标签 42 | print('Found %s texts.' % len(texts)) 43 | 44 | # 将新闻样本转换为张量张量 45 | from keras.preprocessing.text import Tokenizer 46 | from keras.preprocessing.sequence import pad_sequences 47 | tokenizer = Tokenizer(nb_words=MAX_NB_WORDS) 48 | # 只保留新闻文本最常见的20000个词汇 49 | tokenizer.fit_on_texts(texts) 50 | # 根据文本列表更新内部词汇表 51 | sequences = tokenizer.texts_to_sequences(texts) 52 | # 将文本转为序列 53 | word_index = tokenizer.word_index 54 | # 字典,将词汇表的词汇映射为下标 55 | print('Found %s unique tokens.' % len(word_index)) 56 | data = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH) 57 | # 每个新闻文本最多保留1000个词,用这1000个词来判断新闻文本的类别 58 | labels = to_categorical(np.asarray(labels)) 59 | # 将类别向量(从0到nb_classes的整数向量)映射为二值类别矩阵 60 | print('Shape of data tensor:', data.shape) 61 | print('Shape of label tensor:', labels.shape) 62 | 63 | # 将新闻样本分割成一个训练集和一个验证集。 64 | indices = np.arange(data.shape[0]) 65 | np.random.shuffle(indices) 66 | # 打乱新闻样本 67 | data = data[indices] 68 | labels = labels[indices] 69 | nb_validation_samples = int(VALIDATION_SPLIT * data.shape[0]) 70 | x_train = data[:-nb_validation_samples] 71 | y_train = labels[:-nb_validation_samples] 72 | x_val = data[-nb_validation_samples:] 73 | y_val = labels[-nb_validation_samples:] 74 | 75 | # 从GloVe文件中解析出每个词和它所对应的词向量,并用字典的方式存储 76 | embeddings_index = {} 77 | f = open(os.path.join(GLOVE_DIR, 'glove.6B.100d.txt'),'r',encoding='UTF-8') 78 | for line in f: 79 | values = line.split() 80 | word = values[0] 81 | coefs = np.asarray(values[1:], dtype='float32') 82 | embeddings_index[word] = coefs 83 | # 每一行的第一个元素作为键,后面的元素全部作为值 84 | f.close() 85 | print('Found %s word vectors.' % len(embeddings_index)) 86 | print('Found %s unique tokens.' % len(word_index)) 87 | 88 | # 我们可以根据GloVe文件解析得到的字典生成上文所定义的词向量矩阵,第i列表示词索引为i的词的词向量。 89 | embedding_matrix = np.zeros((len(word_index) + 1, EMBEDDING_DIM)) 90 | # 词向量矩阵初始化为0,词汇数*词汇维度 91 | for word, i in word_index.items(): 92 | embedding_vector = embeddings_index.get(word) 93 | if embedding_vector is not None: 94 | embedding_matrix[i] = embedding_vector 95 | 96 | # 词向量矩阵加载到Embedding层 97 | embedding_layer = Embedding(len(word_index) + 1, # 字典长度,即输入数据最大下标+1 98 | EMBEDDING_DIM, #全连接嵌入的维度 99 | weights=[embedding_matrix], 100 | input_length=MAX_SEQUENCE_LENGTH, 101 | trainable=False) 102 | 103 | # 训练一个一维的卷积网络 104 | sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32') 105 | embedded_sequences = embedding_layer(sequence_input) 106 | x = Conv1D(128, 5, activation='relu')(embedded_sequences) 107 | x = MaxPooling1D(5)(x) 108 | x = Conv1D(128, 5, activation='relu')(x) 109 | x = MaxPooling1D(5)(x) 110 | x = Conv1D(128, 5, activation='relu')(x) 111 | x = MaxPooling1D(35)(x) # global max pooling 112 | x = Flatten()(x) 113 | x = Dense(128, activation='relu')(x) 114 | preds = Dense(len(labels_index), activation='softmax')(x) 115 | model = Model(sequence_input, preds) 116 | model.compile(loss='categorical_crossentropy', 117 | optimizer='rmsprop', 118 | metrics=['acc']) 119 | model.fit(x_train, y_train, validation_data=(x_val, y_val), 120 | epochs=20, batch_size=128) 121 | # 可以通过一些正则化机制(如退出)或通过微调Embedding层来更长时间地训练更高的精度。 122 | 123 | # 若不使用预先训练的词语: Embedding从头开始初始化我们的层,并在训练期间学习它的权重。 124 | # 这只需要替换Embedding图层(实际上,预训练的词向量引入了外部语义信息,往往对模型很有帮助): 125 | #embedding_layer = Embedding(len(word_index) + 1, 126 | #EMBEDDING_DIM, 127 | #input_length=MAX_SEQUENCE_LENGTH) -------------------------------------------------------------------------------- /19.ImageDataGenerator.py: -------------------------------------------------------------------------------- 1 | from keras.datasets import cifar10 2 | from keras.utils import np_utils 3 | from keras.preprocessing.image import ImageDataGenerator 4 | from keras.models import Sequential 5 | from keras.layers import Dense, Dropout, Activation, Flatten 6 | from keras.layers import Convolution2D, MaxPooling2D 7 | from keras.optimizers import SGD 8 | batch_size = 32 9 | nb_classes = 10 10 | nb_epoch = 20 11 | kernel_size = (3, 3) 12 | (x_train, y_train), (x_val, y_val) = cifar10.load_data() 13 | y_train = np_utils.to_categorical(y_train, nb_classes) 14 | y_val = np_utils.to_categorical(y_val, nb_classes) 15 | 16 | model = Sequential() 17 | model.add(Convolution2D(32, kernel_size, padding='same', 18 | input_shape=x_train.shape[1:])) 19 | model.add(Activation('relu')) 20 | model.add(Convolution2D(32, kernel_size)) 21 | model.add(Activation('relu')) 22 | model.add(MaxPooling2D(pool_size=(2, 2))) 23 | model.add(Dropout(0.25)) 24 | model.add(Convolution2D(64,kernel_size, padding='same')) 25 | model.add(Activation('relu')) 26 | model.add(Convolution2D(64, kernel_size)) 27 | model.add(Activation('relu')) 28 | model.add(MaxPooling2D(pool_size=(2, 2))) 29 | model.add(Dropout(0.25)) 30 | model.add(Flatten()) 31 | model.add(Dense(512)) 32 | model.add(Activation('relu')) 33 | model.add(Dropout(0.5)) 34 | model.add(Dense(nb_classes)) 35 | model.add(Activation('softmax')) 36 | model.summary() 37 | sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) 38 | model.compile(loss='categorical_crossentropy', 39 | optimizer=sgd, 40 | metrics=['accuracy']) 41 | 42 | datagen = ImageDataGenerator( 43 | featurewise_center=True, 44 | featurewise_std_normalization=True, 45 | rotation_range=20, 46 | width_shift_range=0.2, 47 | height_shift_range=0.2, 48 | horizontal_flip=True, 49 | rescale=1./255) 50 | 51 | # 计算依赖于数据的变换所需要的统计信息(均值方差等), 52 | # 只有使用featurewise_center,featurewise_std_normalization或zca_whitening时需要此函数。 53 | datagen.fit(x_train) 54 | 55 | '''fit_generator接收numpy数组和标签为参数时使用flow''' 56 | model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size), 57 | steps_per_epoch=x_train.shape[0]//batch_size+1, 58 | validation_data=(x_val,y_val), 59 | epochs=nb_epoch) 60 | 61 | '''fit_generator以文件夹路径为参数时使用flow_from_directory''' 62 | # train_datagen = ImageDataGenerator( 63 | # rescale=1./255, 64 | # shear_range=0.2, 65 | # zoom_range=0.2, 66 | # horizontal_flip=True) 67 | # train_generator = train_datagen.flow_from_directory( 68 | # 'data/train', 69 | # target_size=(150, 150), 70 | # batch_size=32, 71 | # class_mode='binary') 72 | # val_datagen = ImageDataGenerator(rescale=1./255) 73 | # validation_generator = val_datagen.flow_from_directory( 74 | # 'data/validation', 75 | # target_size=(150, 150), 76 | # batch_size=32, 77 | # class_mode='binary') 78 | # model.fit_generator( 79 | # train_generator, 80 | # steps_per_epoch=x_train.shape[0]//batch_size+1, 81 | # epochs=nb_epoch, 82 | # validation_data=validation_generator,) 83 | 84 | '''evalute_geneator,主要用于评价模型之前对图片做一些处理''' 85 | val_datagen = ImageDataGenerator(rescale=1./255) 86 | val_generator = datagen.flow( 87 | x_val, y_val, 88 | batch_size=batch_size, 89 | ) 90 | score=model.evaluate_generator(val_generator,steps=x_val.shape[0] // batch_size+1) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Keras_Demo 2 | 3 | 这是Keras的一些简单的Demo 4 | 5 | - [01.多层感知机实现及模型的保存](01.mnist_mpl.py) 6 | - [02.模型的加载](02.load_model.py) 7 | - [03.绘制精度和损失曲线](03.plot_acc_loss.py) 8 | - [04.卷积神经网络实现](04.mnist_cnn.py) 9 | - [05.CIFAR10_cnn](05.cifar10_cnn.py) 10 | - [06.mnist_lstm](06.mnist_lstm.py) 11 | - [07.基于Vgg16模型(含全连接层)的图片识别)](07.vgg-16.py) 12 | - [08.卷积滤波器可视化](08.conv_filter_visualization.py) 13 | - [09.变分自动编码器](09.variational_autoencoder.py) 14 | - [10.迁移学习fine-tuning](10.mnist_transfer_cnn.py) 15 | - [11.Keras和Tensorflow联合使用](11.Keras_with_tensorflow.py) 16 | - [12.使用sklearn wrapper进行的参数搜索](12.mnist_sklearn_wrapper.py) 17 | - [13.基于InceptionV3模型(不含全连接层)的迁移学习应用](13.finetune_InceptionV3.py) 18 | - [14.单隐藏层自编码器](14.single_autoencoder.py) 19 | - [15.多隐藏层自编码器](15.multi_autoencoder.py) 20 | - [16.卷积自编码器](16.Convolutional_autoencoder.py) 21 | - [17.图片去噪处理](17.Image_denoising.py) 22 | - [18.Glove词向量进行新闻分类](18.GloVe_word_embeddings.py) 23 | 24 | 25 | 该仓库会不定期上传Keras的简单Demo,如有错误或补充,欢迎指出 26 | -------------------------------------------------------------------------------- /elephant.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/elephant.jpg -------------------------------------------------------------------------------- /mnist-mpl.h5: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/mnist-mpl.h5 -------------------------------------------------------------------------------- /model-cnn.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/model-cnn.png -------------------------------------------------------------------------------- /model.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/model.png -------------------------------------------------------------------------------- /plot_acc_lost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/plot_acc_lost.png -------------------------------------------------------------------------------- /stitched_filters_8x8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Zheng-Wenkai/Keras_Demo/f58301bbd0ed1ced97919549affc6e72efa053ec/stitched_filters_8x8.png -------------------------------------------------------------------------------- /synset_words.txt: -------------------------------------------------------------------------------- 1 | n01440764 tench, Tinca tinca 2 | n01443537 goldfish, Carassius auratus 3 | n01484850 great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias 4 | n01491361 tiger shark, Galeocerdo cuvieri 5 | n01494475 hammerhead, hammerhead shark 6 | n01496331 electric ray, crampfish, numbfish, torpedo 7 | n01498041 stingray 8 | n01514668 cock 9 | n01514859 hen 10 | n01518878 ostrich, Struthio camelus 11 | n01530575 brambling, Fringilla montifringilla 12 | n01531178 goldfinch, Carduelis carduelis 13 | n01532829 house finch, linnet, Carpodacus mexicanus 14 | n01534433 junco, snowbird 15 | n01537544 indigo bunting, indigo finch, indigo bird, Passerina cyanea 16 | n01558993 robin, American robin, Turdus migratorius 17 | n01560419 bulbul 18 | n01580077 jay 19 | n01582220 magpie 20 | n01592084 chickadee 21 | n01601694 water ouzel, dipper 22 | n01608432 kite 23 | n01614925 bald eagle, American eagle, Haliaeetus leucocephalus 24 | n01616318 vulture 25 | n01622779 great grey owl, great gray owl, Strix nebulosa 26 | n01629819 European fire salamander, Salamandra salamandra 27 | n01630670 common newt, Triturus vulgaris 28 | n01631663 eft 29 | n01632458 spotted salamander, Ambystoma maculatum 30 | n01632777 axolotl, mud puppy, Ambystoma mexicanum 31 | n01641577 bullfrog, Rana catesbeiana 32 | n01644373 tree frog, tree-frog 33 | n01644900 tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui 34 | n01664065 loggerhead, loggerhead turtle, Caretta caretta 35 | n01665541 leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea 36 | n01667114 mud turtle 37 | n01667778 terrapin 38 | n01669191 box turtle, box tortoise 39 | n01675722 banded gecko 40 | n01677366 common iguana, iguana, Iguana iguana 41 | n01682714 American chameleon, anole, Anolis carolinensis 42 | n01685808 whiptail, whiptail lizard 43 | n01687978 agama 44 | n01688243 frilled lizard, Chlamydosaurus kingi 45 | n01689811 alligator lizard 46 | n01692333 Gila monster, Heloderma suspectum 47 | n01693334 green lizard, Lacerta viridis 48 | n01694178 African chameleon, Chamaeleo chamaeleon 49 | n01695060 Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis 50 | n01697457 African crocodile, Nile crocodile, Crocodylus niloticus 51 | n01698640 American alligator, Alligator mississipiensis 52 | n01704323 triceratops 53 | n01728572 thunder snake, worm snake, Carphophis amoenus 54 | n01728920 ringneck snake, ring-necked snake, ring snake 55 | n01729322 hognose snake, puff adder, sand viper 56 | n01729977 green snake, grass snake 57 | n01734418 king snake, kingsnake 58 | n01735189 garter snake, grass snake 59 | n01737021 water snake 60 | n01739381 vine snake 61 | n01740131 night snake, Hypsiglena torquata 62 | n01742172 boa constrictor, Constrictor constrictor 63 | n01744401 rock python, rock snake, Python sebae 64 | n01748264 Indian cobra, Naja naja 65 | n01749939 green mamba 66 | n01751748 sea snake 67 | n01753488 horned viper, cerastes, sand viper, horned asp, Cerastes cornutus 68 | n01755581 diamondback, diamondback rattlesnake, Crotalus adamanteus 69 | n01756291 sidewinder, horned rattlesnake, Crotalus cerastes 70 | n01768244 trilobite 71 | n01770081 harvestman, daddy longlegs, Phalangium opilio 72 | n01770393 scorpion 73 | n01773157 black and gold garden spider, Argiope aurantia 74 | n01773549 barn spider, Araneus cavaticus 75 | n01773797 garden spider, Aranea diademata 76 | n01774384 black widow, Latrodectus mactans 77 | n01774750 tarantula 78 | n01775062 wolf spider, hunting spider 79 | n01776313 tick 80 | n01784675 centipede 81 | n01795545 black grouse 82 | n01796340 ptarmigan 83 | n01797886 ruffed grouse, partridge, Bonasa umbellus 84 | n01798484 prairie chicken, prairie grouse, prairie fowl 85 | n01806143 peacock 86 | n01806567 quail 87 | n01807496 partridge 88 | n01817953 African grey, African gray, Psittacus erithacus 89 | n01818515 macaw 90 | n01819313 sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita 91 | n01820546 lorikeet 92 | n01824575 coucal 93 | n01828970 bee eater 94 | n01829413 hornbill 95 | n01833805 hummingbird 96 | n01843065 jacamar 97 | n01843383 toucan 98 | n01847000 drake 99 | n01855032 red-breasted merganser, Mergus serrator 100 | n01855672 goose 101 | n01860187 black swan, Cygnus atratus 102 | n01871265 tusker 103 | n01872401 echidna, spiny anteater, anteater 104 | n01873310 platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus 105 | n01877812 wallaby, brush kangaroo 106 | n01882714 koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus 107 | n01883070 wombat 108 | n01910747 jellyfish 109 | n01914609 sea anemone, anemone 110 | n01917289 brain coral 111 | n01924916 flatworm, platyhelminth 112 | n01930112 nematode, nematode worm, roundworm 113 | n01943899 conch 114 | n01944390 snail 115 | n01945685 slug 116 | n01950731 sea slug, nudibranch 117 | n01955084 chiton, coat-of-mail shell, sea cradle, polyplacophore 118 | n01968897 chambered nautilus, pearly nautilus, nautilus 119 | n01978287 Dungeness crab, Cancer magister 120 | n01978455 rock crab, Cancer irroratus 121 | n01980166 fiddler crab 122 | n01981276 king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica 123 | n01983481 American lobster, Northern lobster, Maine lobster, Homarus americanus 124 | n01984695 spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish 125 | n01985128 crayfish, crawfish, crawdad, crawdaddy 126 | n01986214 hermit crab 127 | n01990800 isopod 128 | n02002556 white stork, Ciconia ciconia 129 | n02002724 black stork, Ciconia nigra 130 | n02006656 spoonbill 131 | n02007558 flamingo 132 | n02009229 little blue heron, Egretta caerulea 133 | n02009912 American egret, great white heron, Egretta albus 134 | n02011460 bittern 135 | n02012849 crane 136 | n02013706 limpkin, Aramus pictus 137 | n02017213 European gallinule, Porphyrio porphyrio 138 | n02018207 American coot, marsh hen, mud hen, water hen, Fulica americana 139 | n02018795 bustard 140 | n02025239 ruddy turnstone, Arenaria interpres 141 | n02027492 red-backed sandpiper, dunlin, Erolia alpina 142 | n02028035 redshank, Tringa totanus 143 | n02033041 dowitcher 144 | n02037110 oystercatcher, oyster catcher 145 | n02051845 pelican 146 | n02056570 king penguin, Aptenodytes patagonica 147 | n02058221 albatross, mollymawk 148 | n02066245 grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus 149 | n02071294 killer whale, killer, orca, grampus, sea wolf, Orcinus orca 150 | n02074367 dugong, Dugong dugon 151 | n02077923 sea lion 152 | n02085620 Chihuahua 153 | n02085782 Japanese spaniel 154 | n02085936 Maltese dog, Maltese terrier, Maltese 155 | n02086079 Pekinese, Pekingese, Peke 156 | n02086240 Shih-Tzu 157 | n02086646 Blenheim spaniel 158 | n02086910 papillon 159 | n02087046 toy terrier 160 | n02087394 Rhodesian ridgeback 161 | n02088094 Afghan hound, Afghan 162 | n02088238 basset, basset hound 163 | n02088364 beagle 164 | n02088466 bloodhound, sleuthhound 165 | n02088632 bluetick 166 | n02089078 black-and-tan coonhound 167 | n02089867 Walker hound, Walker foxhound 168 | n02089973 English foxhound 169 | n02090379 redbone 170 | n02090622 borzoi, Russian wolfhound 171 | n02090721 Irish wolfhound 172 | n02091032 Italian greyhound 173 | n02091134 whippet 174 | n02091244 Ibizan hound, Ibizan Podenco 175 | n02091467 Norwegian elkhound, elkhound 176 | n02091635 otterhound, otter hound 177 | n02091831 Saluki, gazelle hound 178 | n02092002 Scottish deerhound, deerhound 179 | n02092339 Weimaraner 180 | n02093256 Staffordshire bullterrier, Staffordshire bull terrier 181 | n02093428 American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier 182 | n02093647 Bedlington terrier 183 | n02093754 Border terrier 184 | n02093859 Kerry blue terrier 185 | n02093991 Irish terrier 186 | n02094114 Norfolk terrier 187 | n02094258 Norwich terrier 188 | n02094433 Yorkshire terrier 189 | n02095314 wire-haired fox terrier 190 | n02095570 Lakeland terrier 191 | n02095889 Sealyham terrier, Sealyham 192 | n02096051 Airedale, Airedale terrier 193 | n02096177 cairn, cairn terrier 194 | n02096294 Australian terrier 195 | n02096437 Dandie Dinmont, Dandie Dinmont terrier 196 | n02096585 Boston bull, Boston terrier 197 | n02097047 miniature schnauzer 198 | n02097130 giant schnauzer 199 | n02097209 standard schnauzer 200 | n02097298 Scotch terrier, Scottish terrier, Scottie 201 | n02097474 Tibetan terrier, chrysanthemum dog 202 | n02097658 silky terrier, Sydney silky 203 | n02098105 soft-coated wheaten terrier 204 | n02098286 West Highland white terrier 205 | n02098413 Lhasa, Lhasa apso 206 | n02099267 flat-coated retriever 207 | n02099429 curly-coated retriever 208 | n02099601 golden retriever 209 | n02099712 Labrador retriever 210 | n02099849 Chesapeake Bay retriever 211 | n02100236 German short-haired pointer 212 | n02100583 vizsla, Hungarian pointer 213 | n02100735 English setter 214 | n02100877 Irish setter, red setter 215 | n02101006 Gordon setter 216 | n02101388 Brittany spaniel 217 | n02101556 clumber, clumber spaniel 218 | n02102040 English springer, English springer spaniel 219 | n02102177 Welsh springer spaniel 220 | n02102318 cocker spaniel, English cocker spaniel, cocker 221 | n02102480 Sussex spaniel 222 | n02102973 Irish water spaniel 223 | n02104029 kuvasz 224 | n02104365 schipperke 225 | n02105056 groenendael 226 | n02105162 malinois 227 | n02105251 briard 228 | n02105412 kelpie 229 | n02105505 komondor 230 | n02105641 Old English sheepdog, bobtail 231 | n02105855 Shetland sheepdog, Shetland sheep dog, Shetland 232 | n02106030 collie 233 | n02106166 Border collie 234 | n02106382 Bouvier des Flandres, Bouviers des Flandres 235 | n02106550 Rottweiler 236 | n02106662 German shepherd, German shepherd dog, German police dog, alsatian 237 | n02107142 Doberman, Doberman pinscher 238 | n02107312 miniature pinscher 239 | n02107574 Greater Swiss Mountain dog 240 | n02107683 Bernese mountain dog 241 | n02107908 Appenzeller 242 | n02108000 EntleBucher 243 | n02108089 boxer 244 | n02108422 bull mastiff 245 | n02108551 Tibetan mastiff 246 | n02108915 French bulldog 247 | n02109047 Great Dane 248 | n02109525 Saint Bernard, St Bernard 249 | n02109961 Eskimo dog, husky 250 | n02110063 malamute, malemute, Alaskan malamute 251 | n02110185 Siberian husky 252 | n02110341 dalmatian, coach dog, carriage dog 253 | n02110627 affenpinscher, monkey pinscher, monkey dog 254 | n02110806 basenji 255 | n02110958 pug, pug-dog 256 | n02111129 Leonberg 257 | n02111277 Newfoundland, Newfoundland dog 258 | n02111500 Great Pyrenees 259 | n02111889 Samoyed, Samoyede 260 | n02112018 Pomeranian 261 | n02112137 chow, chow chow 262 | n02112350 keeshond 263 | n02112706 Brabancon griffon 264 | n02113023 Pembroke, Pembroke Welsh corgi 265 | n02113186 Cardigan, Cardigan Welsh corgi 266 | n02113624 toy poodle 267 | n02113712 miniature poodle 268 | n02113799 standard poodle 269 | n02113978 Mexican hairless 270 | n02114367 timber wolf, grey wolf, gray wolf, Canis lupus 271 | n02114548 white wolf, Arctic wolf, Canis lupus tundrarum 272 | n02114712 red wolf, maned wolf, Canis rufus, Canis niger 273 | n02114855 coyote, prairie wolf, brush wolf, Canis latrans 274 | n02115641 dingo, warrigal, warragal, Canis dingo 275 | n02115913 dhole, Cuon alpinus 276 | n02116738 African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus 277 | n02117135 hyena, hyaena 278 | n02119022 red fox, Vulpes vulpes 279 | n02119789 kit fox, Vulpes macrotis 280 | n02120079 Arctic fox, white fox, Alopex lagopus 281 | n02120505 grey fox, gray fox, Urocyon cinereoargenteus 282 | n02123045 tabby, tabby cat 283 | n02123159 tiger cat 284 | n02123394 Persian cat 285 | n02123597 Siamese cat, Siamese 286 | n02124075 Egyptian cat 287 | n02125311 cougar, puma, catamount, mountain lion, painter, panther, Felis concolor 288 | n02127052 lynx, catamount 289 | n02128385 leopard, Panthera pardus 290 | n02128757 snow leopard, ounce, Panthera uncia 291 | n02128925 jaguar, panther, Panthera onca, Felis onca 292 | n02129165 lion, king of beasts, Panthera leo 293 | n02129604 tiger, Panthera tigris 294 | n02130308 cheetah, chetah, Acinonyx jubatus 295 | n02132136 brown bear, bruin, Ursus arctos 296 | n02133161 American black bear, black bear, Ursus americanus, Euarctos americanus 297 | n02134084 ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus 298 | n02134418 sloth bear, Melursus ursinus, Ursus ursinus 299 | n02137549 mongoose 300 | n02138441 meerkat, mierkat 301 | n02165105 tiger beetle 302 | n02165456 ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle 303 | n02167151 ground beetle, carabid beetle 304 | n02168699 long-horned beetle, longicorn, longicorn beetle 305 | n02169497 leaf beetle, chrysomelid 306 | n02172182 dung beetle 307 | n02174001 rhinoceros beetle 308 | n02177972 weevil 309 | n02190166 fly 310 | n02206856 bee 311 | n02219486 ant, emmet, pismire 312 | n02226429 grasshopper, hopper 313 | n02229544 cricket 314 | n02231487 walking stick, walkingstick, stick insect 315 | n02233338 cockroach, roach 316 | n02236044 mantis, mantid 317 | n02256656 cicada, cicala 318 | n02259212 leafhopper 319 | n02264363 lacewing, lacewing fly 320 | n02268443 dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk 321 | n02268853 damselfly 322 | n02276258 admiral 323 | n02277742 ringlet, ringlet butterfly 324 | n02279972 monarch, monarch butterfly, milkweed butterfly, Danaus plexippus 325 | n02280649 cabbage butterfly 326 | n02281406 sulphur butterfly, sulfur butterfly 327 | n02281787 lycaenid, lycaenid butterfly 328 | n02317335 starfish, sea star 329 | n02319095 sea urchin 330 | n02321529 sea cucumber, holothurian 331 | n02325366 wood rabbit, cottontail, cottontail rabbit 332 | n02326432 hare 333 | n02328150 Angora, Angora rabbit 334 | n02342885 hamster 335 | n02346627 porcupine, hedgehog 336 | n02356798 fox squirrel, eastern fox squirrel, Sciurus niger 337 | n02361337 marmot 338 | n02363005 beaver 339 | n02364673 guinea pig, Cavia cobaya 340 | n02389026 sorrel 341 | n02391049 zebra 342 | n02395406 hog, pig, grunter, squealer, Sus scrofa 343 | n02396427 wild boar, boar, Sus scrofa 344 | n02397096 warthog 345 | n02398521 hippopotamus, hippo, river horse, Hippopotamus amphibius 346 | n02403003 ox 347 | n02408429 water buffalo, water ox, Asiatic buffalo, Bubalus bubalis 348 | n02410509 bison 349 | n02412080 ram, tup 350 | n02415577 bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis 351 | n02417914 ibex, Capra ibex 352 | n02422106 hartebeest 353 | n02422699 impala, Aepyceros melampus 354 | n02423022 gazelle 355 | n02437312 Arabian camel, dromedary, Camelus dromedarius 356 | n02437616 llama 357 | n02441942 weasel 358 | n02442845 mink 359 | n02443114 polecat, fitch, foulmart, foumart, Mustela putorius 360 | n02443484 black-footed ferret, ferret, Mustela nigripes 361 | n02444819 otter 362 | n02445715 skunk, polecat, wood pussy 363 | n02447366 badger 364 | n02454379 armadillo 365 | n02457408 three-toed sloth, ai, Bradypus tridactylus 366 | n02480495 orangutan, orang, orangutang, Pongo pygmaeus 367 | n02480855 gorilla, Gorilla gorilla 368 | n02481823 chimpanzee, chimp, Pan troglodytes 369 | n02483362 gibbon, Hylobates lar 370 | n02483708 siamang, Hylobates syndactylus, Symphalangus syndactylus 371 | n02484975 guenon, guenon monkey 372 | n02486261 patas, hussar monkey, Erythrocebus patas 373 | n02486410 baboon 374 | n02487347 macaque 375 | n02488291 langur 376 | n02488702 colobus, colobus monkey 377 | n02489166 proboscis monkey, Nasalis larvatus 378 | n02490219 marmoset 379 | n02492035 capuchin, ringtail, Cebus capucinus 380 | n02492660 howler monkey, howler 381 | n02493509 titi, titi monkey 382 | n02493793 spider monkey, Ateles geoffroyi 383 | n02494079 squirrel monkey, Saimiri sciureus 384 | n02497673 Madagascar cat, ring-tailed lemur, Lemur catta 385 | n02500267 indri, indris, Indri indri, Indri brevicaudatus 386 | n02504013 Indian elephant, Elephas maximus 387 | n02504458 African elephant, Loxodonta africana 388 | n02509815 lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens 389 | n02510455 giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca 390 | n02514041 barracouta, snoek 391 | n02526121 eel 392 | n02536864 coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch 393 | n02606052 rock beauty, Holocanthus tricolor 394 | n02607072 anemone fish 395 | n02640242 sturgeon 396 | n02641379 gar, garfish, garpike, billfish, Lepisosteus osseus 397 | n02643566 lionfish 398 | n02655020 puffer, pufferfish, blowfish, globefish 399 | n02666196 abacus 400 | n02667093 abaya 401 | n02669723 academic gown, academic robe, judge's robe 402 | n02672831 accordion, piano accordion, squeeze box 403 | n02676566 acoustic guitar 404 | n02687172 aircraft carrier, carrier, flattop, attack aircraft carrier 405 | n02690373 airliner 406 | n02692877 airship, dirigible 407 | n02699494 altar 408 | n02701002 ambulance 409 | n02704792 amphibian, amphibious vehicle 410 | n02708093 analog clock 411 | n02727426 apiary, bee house 412 | n02730930 apron 413 | n02747177 ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin 414 | n02749479 assault rifle, assault gun 415 | n02769748 backpack, back pack, knapsack, packsack, rucksack, haversack 416 | n02776631 bakery, bakeshop, bakehouse 417 | n02777292 balance beam, beam 418 | n02782093 balloon 419 | n02783161 ballpoint, ballpoint pen, ballpen, Biro 420 | n02786058 Band Aid 421 | n02787622 banjo 422 | n02788148 bannister, banister, balustrade, balusters, handrail 423 | n02790996 barbell 424 | n02791124 barber chair 425 | n02791270 barbershop 426 | n02793495 barn 427 | n02794156 barometer 428 | n02795169 barrel, cask 429 | n02797295 barrow, garden cart, lawn cart, wheelbarrow 430 | n02799071 baseball 431 | n02802426 basketball 432 | n02804414 bassinet 433 | n02804610 bassoon 434 | n02807133 bathing cap, swimming cap 435 | n02808304 bath towel 436 | n02808440 bathtub, bathing tub, bath, tub 437 | n02814533 beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon 438 | n02814860 beacon, lighthouse, beacon light, pharos 439 | n02815834 beaker 440 | n02817516 bearskin, busby, shako 441 | n02823428 beer bottle 442 | n02823750 beer glass 443 | n02825657 bell cote, bell cot 444 | n02834397 bib 445 | n02835271 bicycle-built-for-two, tandem bicycle, tandem 446 | n02837789 bikini, two-piece 447 | n02840245 binder, ring-binder 448 | n02841315 binoculars, field glasses, opera glasses 449 | n02843684 birdhouse 450 | n02859443 boathouse 451 | n02860847 bobsled, bobsleigh, bob 452 | n02865351 bolo tie, bolo, bola tie, bola 453 | n02869837 bonnet, poke bonnet 454 | n02870880 bookcase 455 | n02871525 bookshop, bookstore, bookstall 456 | n02877765 bottlecap 457 | n02879718 bow 458 | n02883205 bow tie, bow-tie, bowtie 459 | n02892201 brass, memorial tablet, plaque 460 | n02892767 brassiere, bra, bandeau 461 | n02894605 breakwater, groin, groyne, mole, bulwark, seawall, jetty 462 | n02895154 breastplate, aegis, egis 463 | n02906734 broom 464 | n02909870 bucket, pail 465 | n02910353 buckle 466 | n02916936 bulletproof vest 467 | n02917067 bullet train, bullet 468 | n02927161 butcher shop, meat market 469 | n02930766 cab, hack, taxi, taxicab 470 | n02939185 caldron, cauldron 471 | n02948072 candle, taper, wax light 472 | n02950826 cannon 473 | n02951358 canoe 474 | n02951585 can opener, tin opener 475 | n02963159 cardigan 476 | n02965783 car mirror 477 | n02966193 carousel, carrousel, merry-go-round, roundabout, whirligig 478 | n02966687 carpenter's kit, tool kit 479 | n02971356 carton 480 | n02974003 car wheel 481 | n02977058 cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM 482 | n02978881 cassette 483 | n02979186 cassette player 484 | n02980441 castle 485 | n02981792 catamaran 486 | n02988304 CD player 487 | n02992211 cello, violoncello 488 | n02992529 cellular telephone, cellular phone, cellphone, cell, mobile phone 489 | n02999410 chain 490 | n03000134 chainlink fence 491 | n03000247 chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour 492 | n03000684 chain saw, chainsaw 493 | n03014705 chest 494 | n03016953 chiffonier, commode 495 | n03017168 chime, bell, gong 496 | n03018349 china cabinet, china closet 497 | n03026506 Christmas stocking 498 | n03028079 church, church building 499 | n03032252 cinema, movie theater, movie theatre, movie house, picture palace 500 | n03041632 cleaver, meat cleaver, chopper 501 | n03042490 cliff dwelling 502 | n03045698 cloak 503 | n03047690 clog, geta, patten, sabot 504 | n03062245 cocktail shaker 505 | n03063599 coffee mug 506 | n03063689 coffeepot 507 | n03065424 coil, spiral, volute, whorl, helix 508 | n03075370 combination lock 509 | n03085013 computer keyboard, keypad 510 | n03089624 confectionery, confectionary, candy store 511 | n03095699 container ship, containership, container vessel 512 | n03100240 convertible 513 | n03109150 corkscrew, bottle screw 514 | n03110669 cornet, horn, trumpet, trump 515 | n03124043 cowboy boot 516 | n03124170 cowboy hat, ten-gallon hat 517 | n03125729 cradle 518 | n03126707 crane 519 | n03127747 crash helmet 520 | n03127925 crate 521 | n03131574 crib, cot 522 | n03133878 Crock Pot 523 | n03134739 croquet ball 524 | n03141823 crutch 525 | n03146219 cuirass 526 | n03160309 dam, dike, dyke 527 | n03179701 desk 528 | n03180011 desktop computer 529 | n03187595 dial telephone, dial phone 530 | n03188531 diaper, nappy, napkin 531 | n03196217 digital clock 532 | n03197337 digital watch 533 | n03201208 dining table, board 534 | n03207743 dishrag, dishcloth 535 | n03207941 dishwasher, dish washer, dishwashing machine 536 | n03208938 disk brake, disc brake 537 | n03216828 dock, dockage, docking facility 538 | n03218198 dogsled, dog sled, dog sleigh 539 | n03220513 dome 540 | n03223299 doormat, welcome mat 541 | n03240683 drilling platform, offshore rig 542 | n03249569 drum, membranophone, tympan 543 | n03250847 drumstick 544 | n03255030 dumbbell 545 | n03259280 Dutch oven 546 | n03271574 electric fan, blower 547 | n03272010 electric guitar 548 | n03272562 electric locomotive 549 | n03290653 entertainment center 550 | n03291819 envelope 551 | n03297495 espresso maker 552 | n03314780 face powder 553 | n03325584 feather boa, boa 554 | n03337140 file, file cabinet, filing cabinet 555 | n03344393 fireboat 556 | n03345487 fire engine, fire truck 557 | n03347037 fire screen, fireguard 558 | n03355925 flagpole, flagstaff 559 | n03372029 flute, transverse flute 560 | n03376595 folding chair 561 | n03379051 football helmet 562 | n03384352 forklift 563 | n03388043 fountain 564 | n03388183 fountain pen 565 | n03388549 four-poster 566 | n03393912 freight car 567 | n03394916 French horn, horn 568 | n03400231 frying pan, frypan, skillet 569 | n03404251 fur coat 570 | n03417042 garbage truck, dustcart 571 | n03424325 gasmask, respirator, gas helmet 572 | n03425413 gas pump, gasoline pump, petrol pump, island dispenser 573 | n03443371 goblet 574 | n03444034 go-kart 575 | n03445777 golf ball 576 | n03445924 golfcart, golf cart 577 | n03447447 gondola 578 | n03447721 gong, tam-tam 579 | n03450230 gown 580 | n03452741 grand piano, grand 581 | n03457902 greenhouse, nursery, glasshouse 582 | n03459775 grille, radiator grille 583 | n03461385 grocery store, grocery, food market, market 584 | n03467068 guillotine 585 | n03476684 hair slide 586 | n03476991 hair spray 587 | n03478589 half track 588 | n03481172 hammer 589 | n03482405 hamper 590 | n03483316 hand blower, blow dryer, blow drier, hair dryer, hair drier 591 | n03485407 hand-held computer, hand-held microcomputer 592 | n03485794 handkerchief, hankie, hanky, hankey 593 | n03492542 hard disc, hard disk, fixed disk 594 | n03494278 harmonica, mouth organ, harp, mouth harp 595 | n03495258 harp 596 | n03496892 harvester, reaper 597 | n03498962 hatchet 598 | n03527444 holster 599 | n03529860 home theater, home theatre 600 | n03530642 honeycomb 601 | n03532672 hook, claw 602 | n03534580 hoopskirt, crinoline 603 | n03535780 horizontal bar, high bar 604 | n03538406 horse cart, horse-cart 605 | n03544143 hourglass 606 | n03584254 iPod 607 | n03584829 iron, smoothing iron 608 | n03590841 jack-o'-lantern 609 | n03594734 jean, blue jean, denim 610 | n03594945 jeep, landrover 611 | n03595614 jersey, T-shirt, tee shirt 612 | n03598930 jigsaw puzzle 613 | n03599486 jinrikisha, ricksha, rickshaw 614 | n03602883 joystick 615 | n03617480 kimono 616 | n03623198 knee pad 617 | n03627232 knot 618 | n03630383 lab coat, laboratory coat 619 | n03633091 ladle 620 | n03637318 lampshade, lamp shade 621 | n03642806 laptop, laptop computer 622 | n03649909 lawn mower, mower 623 | n03657121 lens cap, lens cover 624 | n03658185 letter opener, paper knife, paperknife 625 | n03661043 library 626 | n03662601 lifeboat 627 | n03666591 lighter, light, igniter, ignitor 628 | n03670208 limousine, limo 629 | n03673027 liner, ocean liner 630 | n03676483 lipstick, lip rouge 631 | n03680355 Loafer 632 | n03690938 lotion 633 | n03691459 loudspeaker, speaker, speaker unit, loudspeaker system, speaker system 634 | n03692522 loupe, jeweler's loupe 635 | n03697007 lumbermill, sawmill 636 | n03706229 magnetic compass 637 | n03709823 mailbag, postbag 638 | n03710193 mailbox, letter box 639 | n03710637 maillot 640 | n03710721 maillot, tank suit 641 | n03717622 manhole cover 642 | n03720891 maraca 643 | n03721384 marimba, xylophone 644 | n03724870 mask 645 | n03729826 matchstick 646 | n03733131 maypole 647 | n03733281 maze, labyrinth 648 | n03733805 measuring cup 649 | n03742115 medicine chest, medicine cabinet 650 | n03743016 megalith, megalithic structure 651 | n03759954 microphone, mike 652 | n03761084 microwave, microwave oven 653 | n03763968 military uniform 654 | n03764736 milk can 655 | n03769881 minibus 656 | n03770439 miniskirt, mini 657 | n03770679 minivan 658 | n03773504 missile 659 | n03775071 mitten 660 | n03775546 mixing bowl 661 | n03776460 mobile home, manufactured home 662 | n03777568 Model T 663 | n03777754 modem 664 | n03781244 monastery 665 | n03782006 monitor 666 | n03785016 moped 667 | n03786901 mortar 668 | n03787032 mortarboard 669 | n03788195 mosque 670 | n03788365 mosquito net 671 | n03791053 motor scooter, scooter 672 | n03792782 mountain bike, all-terrain bike, off-roader 673 | n03792972 mountain tent 674 | n03793489 mouse, computer mouse 675 | n03794056 mousetrap 676 | n03796401 moving van 677 | n03803284 muzzle 678 | n03804744 nail 679 | n03814639 neck brace 680 | n03814906 necklace 681 | n03825788 nipple 682 | n03832673 notebook, notebook computer 683 | n03837869 obelisk 684 | n03838899 oboe, hautboy, hautbois 685 | n03840681 ocarina, sweet potato 686 | n03841143 odometer, hodometer, mileometer, milometer 687 | n03843555 oil filter 688 | n03854065 organ, pipe organ 689 | n03857828 oscilloscope, scope, cathode-ray oscilloscope, CRO 690 | n03866082 overskirt 691 | n03868242 oxcart 692 | n03868863 oxygen mask 693 | n03871628 packet 694 | n03873416 paddle, boat paddle 695 | n03874293 paddlewheel, paddle wheel 696 | n03874599 padlock 697 | n03876231 paintbrush 698 | n03877472 pajama, pyjama, pj's, jammies 699 | n03877845 palace 700 | n03884397 panpipe, pandean pipe, syrinx 701 | n03887697 paper towel 702 | n03888257 parachute, chute 703 | n03888605 parallel bars, bars 704 | n03891251 park bench 705 | n03891332 parking meter 706 | n03895866 passenger car, coach, carriage 707 | n03899768 patio, terrace 708 | n03902125 pay-phone, pay-station 709 | n03903868 pedestal, plinth, footstall 710 | n03908618 pencil box, pencil case 711 | n03908714 pencil sharpener 712 | n03916031 perfume, essence 713 | n03920288 Petri dish 714 | n03924679 photocopier 715 | n03929660 pick, plectrum, plectron 716 | n03929855 pickelhaube 717 | n03930313 picket fence, paling 718 | n03930630 pickup, pickup truck 719 | n03933933 pier 720 | n03935335 piggy bank, penny bank 721 | n03937543 pill bottle 722 | n03938244 pillow 723 | n03942813 ping-pong ball 724 | n03944341 pinwheel 725 | n03947888 pirate, pirate ship 726 | n03950228 pitcher, ewer 727 | n03954731 plane, carpenter's plane, woodworking plane 728 | n03956157 planetarium 729 | n03958227 plastic bag 730 | n03961711 plate rack 731 | n03967562 plow, plough 732 | n03970156 plunger, plumber's helper 733 | n03976467 Polaroid camera, Polaroid Land camera 734 | n03976657 pole 735 | n03977966 police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria 736 | n03980874 poncho 737 | n03982430 pool table, billiard table, snooker table 738 | n03983396 pop bottle, soda bottle 739 | n03991062 pot, flowerpot 740 | n03992509 potter's wheel 741 | n03995372 power drill 742 | n03998194 prayer rug, prayer mat 743 | n04004767 printer 744 | n04005630 prison, prison house 745 | n04008634 projectile, missile 746 | n04009552 projector 747 | n04019541 puck, hockey puck 748 | n04023962 punching bag, punch bag, punching ball, punchball 749 | n04026417 purse 750 | n04033901 quill, quill pen 751 | n04033995 quilt, comforter, comfort, puff 752 | n04037443 racer, race car, racing car 753 | n04039381 racket, racquet 754 | n04040759 radiator 755 | n04041544 radio, wireless 756 | n04044716 radio telescope, radio reflector 757 | n04049303 rain barrel 758 | n04065272 recreational vehicle, RV, R.V. 759 | n04067472 reel 760 | n04069434 reflex camera 761 | n04070727 refrigerator, icebox 762 | n04074963 remote control, remote 763 | n04081281 restaurant, eating house, eating place, eatery 764 | n04086273 revolver, six-gun, six-shooter 765 | n04090263 rifle 766 | n04099969 rocking chair, rocker 767 | n04111531 rotisserie 768 | n04116512 rubber eraser, rubber, pencil eraser 769 | n04118538 rugby ball 770 | n04118776 rule, ruler 771 | n04120489 running shoe 772 | n04125021 safe 773 | n04127249 safety pin 774 | n04131690 saltshaker, salt shaker 775 | n04133789 sandal 776 | n04136333 sarong 777 | n04141076 sax, saxophone 778 | n04141327 scabbard 779 | n04141975 scale, weighing machine 780 | n04146614 school bus 781 | n04147183 schooner 782 | n04149813 scoreboard 783 | n04152593 screen, CRT screen 784 | n04153751 screw 785 | n04154565 screwdriver 786 | n04162706 seat belt, seatbelt 787 | n04179913 sewing machine 788 | n04192698 shield, buckler 789 | n04200800 shoe shop, shoe-shop, shoe store 790 | n04201297 shoji 791 | n04204238 shopping basket 792 | n04204347 shopping cart 793 | n04208210 shovel 794 | n04209133 shower cap 795 | n04209239 shower curtain 796 | n04228054 ski 797 | n04229816 ski mask 798 | n04235860 sleeping bag 799 | n04238763 slide rule, slipstick 800 | n04239074 sliding door 801 | n04243546 slot, one-armed bandit 802 | n04251144 snorkel 803 | n04252077 snowmobile 804 | n04252225 snowplow, snowplough 805 | n04254120 soap dispenser 806 | n04254680 soccer ball 807 | n04254777 sock 808 | n04258138 solar dish, solar collector, solar furnace 809 | n04259630 sombrero 810 | n04263257 soup bowl 811 | n04264628 space bar 812 | n04265275 space heater 813 | n04266014 space shuttle 814 | n04270147 spatula 815 | n04273569 speedboat 816 | n04275548 spider web, spider's web 817 | n04277352 spindle 818 | n04285008 sports car, sport car 819 | n04286575 spotlight, spot 820 | n04296562 stage 821 | n04310018 steam locomotive 822 | n04311004 steel arch bridge 823 | n04311174 steel drum 824 | n04317175 stethoscope 825 | n04325704 stole 826 | n04326547 stone wall 827 | n04328186 stopwatch, stop watch 828 | n04330267 stove 829 | n04332243 strainer 830 | n04335435 streetcar, tram, tramcar, trolley, trolley car 831 | n04336792 stretcher 832 | n04344873 studio couch, day bed 833 | n04346328 stupa, tope 834 | n04347754 submarine, pigboat, sub, U-boat 835 | n04350905 suit, suit of clothes 836 | n04355338 sundial 837 | n04355933 sunglass 838 | n04356056 sunglasses, dark glasses, shades 839 | n04357314 sunscreen, sunblock, sun blocker 840 | n04366367 suspension bridge 841 | n04367480 swab, swob, mop 842 | n04370456 sweatshirt 843 | n04371430 swimming trunks, bathing trunks 844 | n04371774 swing 845 | n04372370 switch, electric switch, electrical switch 846 | n04376876 syringe 847 | n04380533 table lamp 848 | n04389033 tank, army tank, armored combat vehicle, armoured combat vehicle 849 | n04392985 tape player 850 | n04398044 teapot 851 | n04399382 teddy, teddy bear 852 | n04404412 television, television system 853 | n04409515 tennis ball 854 | n04417672 thatch, thatched roof 855 | n04418357 theater curtain, theatre curtain 856 | n04423845 thimble 857 | n04428191 thresher, thrasher, threshing machine 858 | n04429376 throne 859 | n04435653 tile roof 860 | n04442312 toaster 861 | n04443257 tobacco shop, tobacconist shop, tobacconist 862 | n04447861 toilet seat 863 | n04456115 torch 864 | n04458633 totem pole 865 | n04461696 tow truck, tow car, wrecker 866 | n04462240 toyshop 867 | n04465501 tractor 868 | n04467665 trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi 869 | n04476259 tray 870 | n04479046 trench coat 871 | n04482393 tricycle, trike, velocipede 872 | n04483307 trimaran 873 | n04485082 tripod 874 | n04486054 triumphal arch 875 | n04487081 trolleybus, trolley coach, trackless trolley 876 | n04487394 trombone 877 | n04493381 tub, vat 878 | n04501370 turnstile 879 | n04505470 typewriter keyboard 880 | n04507155 umbrella 881 | n04509417 unicycle, monocycle 882 | n04515003 upright, upright piano 883 | n04517823 vacuum, vacuum cleaner 884 | n04522168 vase 885 | n04523525 vault 886 | n04525038 velvet 887 | n04525305 vending machine 888 | n04532106 vestment 889 | n04532670 viaduct 890 | n04536866 violin, fiddle 891 | n04540053 volleyball 892 | n04542943 waffle iron 893 | n04548280 wall clock 894 | n04548362 wallet, billfold, notecase, pocketbook 895 | n04550184 wardrobe, closet, press 896 | n04552348 warplane, military plane 897 | n04553703 washbasin, handbasin, washbowl, lavabo, wash-hand basin 898 | n04554684 washer, automatic washer, washing machine 899 | n04557648 water bottle 900 | n04560804 water jug 901 | n04562935 water tower 902 | n04579145 whiskey jug 903 | n04579432 whistle 904 | n04584207 wig 905 | n04589890 window screen 906 | n04590129 window shade 907 | n04591157 Windsor tie 908 | n04591713 wine bottle 909 | n04592741 wing 910 | n04596742 wok 911 | n04597913 wooden spoon 912 | n04599235 wool, woolen, woollen 913 | n04604644 worm fence, snake fence, snake-rail fence, Virginia fence 914 | n04606251 wreck 915 | n04612504 yawl 916 | n04613696 yurt 917 | n06359193 web site, website, internet site, site 918 | n06596364 comic book 919 | n06785654 crossword puzzle, crossword 920 | n06794110 street sign 921 | n06874185 traffic light, traffic signal, stoplight 922 | n07248320 book jacket, dust cover, dust jacket, dust wrapper 923 | n07565083 menu 924 | n07579787 plate 925 | n07583066 guacamole 926 | n07584110 consomme 927 | n07590611 hot pot, hotpot 928 | n07613480 trifle 929 | n07614500 ice cream, icecream 930 | n07615774 ice lolly, lolly, lollipop, popsicle 931 | n07684084 French loaf 932 | n07693725 bagel, beigel 933 | n07695742 pretzel 934 | n07697313 cheeseburger 935 | n07697537 hotdog, hot dog, red hot 936 | n07711569 mashed potato 937 | n07714571 head cabbage 938 | n07714990 broccoli 939 | n07715103 cauliflower 940 | n07716358 zucchini, courgette 941 | n07716906 spaghetti squash 942 | n07717410 acorn squash 943 | n07717556 butternut squash 944 | n07718472 cucumber, cuke 945 | n07718747 artichoke, globe artichoke 946 | n07720875 bell pepper 947 | n07730033 cardoon 948 | n07734744 mushroom 949 | n07742313 Granny Smith 950 | n07745940 strawberry 951 | n07747607 orange 952 | n07749582 lemon 953 | n07753113 fig 954 | n07753275 pineapple, ananas 955 | n07753592 banana 956 | n07754684 jackfruit, jak, jack 957 | n07760859 custard apple 958 | n07768694 pomegranate 959 | n07802026 hay 960 | n07831146 carbonara 961 | n07836838 chocolate sauce, chocolate syrup 962 | n07860988 dough 963 | n07871810 meat loaf, meatloaf 964 | n07873807 pizza, pizza pie 965 | n07875152 potpie 966 | n07880968 burrito 967 | n07892512 red wine 968 | n07920052 espresso 969 | n07930864 cup 970 | n07932039 eggnog 971 | n09193705 alp 972 | n09229709 bubble 973 | n09246464 cliff, drop, drop-off 974 | n09256479 coral reef 975 | n09288635 geyser 976 | n09332890 lakeside, lakeshore 977 | n09399592 promontory, headland, head, foreland 978 | n09421951 sandbar, sand bar 979 | n09428293 seashore, coast, seacoast, sea-coast 980 | n09468604 valley, vale 981 | n09472597 volcano 982 | n09835506 ballplayer, baseball player 983 | n10148035 groom, bridegroom 984 | n10565667 scuba diver 985 | n11879895 rapeseed 986 | n11939491 daisy 987 | n12057211 yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum 988 | n12144580 corn 989 | n12267677 acorn 990 | n12620546 hip, rose hip, rosehip 991 | n12768682 buckeye, horse chestnut, conker 992 | n12985857 coral fungus 993 | n12998815 agaric 994 | n13037406 gyromitra 995 | n13040303 stinkhorn, carrion fungus 996 | n13044778 earthstar 997 | n13052670 hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa 998 | n13054560 bolete 999 | n13133613 ear, spike, capitulum 1000 | n15075141 toilet tissue, toilet paper, bathroom tissue 1001 | --------------------------------------------------------------------------------