├── 模型说明 ├── functions-of-CNN.py ├── input_data_func.py ├── total_program.py ├── main_program.py ├── VGG16_model.py ├── vgg_preprocess.py └── imagenet_classes.py /模型说明: -------------------------------------------------------------------------------- 1 | # 项目简介——猫狗大战 2 | - 源于kaggle上的一个竞赛项目 3 | - 利用给定的图像数据集,用算法实现猫和狗的识别 4 | 5 | 6 | # 迁移学习 7 | - 利用已经训练好的模型,作为新模型训练的初始化的学习方式 8 | - 优势: 9 | - 所需的样本数量更少 10 | - 模型达到收敛耗时更短 11 | - 比如网络在cifar-10数据集上迭代训练5000次收敛,将一个在cifar-100上训练好的模型迁移至cifar-10上,只需要1000次迭代就可以收敛 12 | - 什么时候适合选择迁移学习 13 | - 当新数据集比较小,且与原数据集相似时 14 | - 当算力比较有限时 15 | - 如何使用 16 | - 当新数据集比较小且和原数据集相似时,只训练softmax层 17 | - 把原模型的softmax层换成自己的softmax层,冻结前面所有层的参数 18 | - 当新数据集不是非常小,而且我们具有一定的算力,可以训练部分参数 19 | - 除了softmax层的调整外,还可以改变模型的后面几层(冻结浅层的参数,训练较深层的参数) 20 | - 具备足够的样本和算力,可以对所有参数进行训练,原来模型的参数则作为初始化的数据 21 | - 迁移学习的规律: 22 | - 随着数据的增加和算力的增加,需要冻结的层数越来越少,参与训练的层数越来越多 23 | - 计算机视觉是经常要用到迁移学习的领域 24 | 25 | # 项目实践:用VGG16模型进行迁移学习 26 | ## 数据准备 27 | - 数据集包括猫和狗的图片各12500张,测试集包括12500张猫狗图片 28 | ## VGG16的TensorFlow的实现 29 | - 定义功能函数 30 | - 定义VGG16模型类 31 | ## VGG16模型复用 32 | - 微调(finetuining):对需要训练的少量层级和相关参数进行重新训练 33 | - trainable参数变动: 34 | - 在进行微调对模型进行重新训练时,对于部分不需要训练的层可以通过设置trainable=False,来确保其在训练过程中不会被修改权值 35 | - 在VGG16类中调整相关代码 36 | - 全连接层的神经元个数:预训练的VGG是在ImageNet数据集上进行的,对1000个类别进行判定,若希望利用已训练模型用于其他分类任务,需要修改最后的全连接层 37 | - 载入权重:复用已经训练好的权重参数,通过load方法将数据以字典的形式读入 38 | ## 数据输入 39 | - data_in_func:数据输入、预处理等相关函数 40 | ## 模型重新训练与保存 41 | ## 预测 -------------------------------------------------------------------------------- /functions-of-CNN.py: -------------------------------------------------------------------------------- 1 | 2 | """定义了卷积层、池化层和全连接层实现的方法函数""" 3 | 4 | import tensorflow as tf 5 | 6 | # 卷积的方法 7 | def conv(self, name, input_data, out_channel): 8 | in_channel = input_data.get_shape()[-1] 9 | with tf.variable_scope(name): 10 | kernel = tf.get_variable('weight', [3, 3, in_channel, out_channel], dtype=tf.float32) 11 | biases = tf.get_varlable('biases', [out_channel], dtype=tf.float32) 12 | conv_res = tf.nn.conv2d(input_data, kernel, [1,1,1,1], padding='SAME') 13 | res = tf.nn.bias_add(conv_res, biases) 14 | out = tf.nn.relu(res, name=name) 15 | return out 16 | 17 | # 全连接层的方法 18 | def fc(self, name, input_data, out_channel): 19 | shape = input_data.get_shape().as_list() 20 | # size定义了作为输入的神经元的个数 21 | # 如果输入是[batch,width,height,channels],则size = w * h * c 22 | if len(shape) == 4: 23 | size = shape[-1] * shape[-2] * shape[-3] 24 | else: 25 | size = shape[1] 26 | # 将输入数据展开成一维列向量 27 | input_data_flat = tf.reshape(input_data, [-1, size]) 28 | with tf.variable_scope(name): 29 | # 其中,size为作为输入的神经元的个数 30 | weights = tf.get_variable(name='weight', shape=[size, out_channel], dtype=tf.float32) 31 | biases = tf.get_variable(name='biases', shape=[out_channel], dtype=tf.float32) 32 | res = tf.matmul(input_data_flat, weights) 33 | out = tf.nn.relu(tf.nn.bias_add(res, biases)) 34 | return out 35 | 36 | # 池化层的方法 37 | def max_pool(self, name, input_data): 38 | out = tf.nn.max_pool(input_data, [1,2,2,1], [1,2,2,1], padding='SAME', name=name) 39 | return out 40 | 41 | -------------------------------------------------------------------------------- /input_data_func.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import tensorflow as tf 4 | 5 | def get_file(file_dir): 6 | images = [] 7 | temp = [] 8 | for root, sub_folders, files in os.walk(file_dir): 9 | for name in files: 10 | images.append(os.path.join(root, name)) 11 | for name in sub_folders: 12 | temp.append(os.path.join(root, name)) 13 | labels = [] 14 | for one_folder in temp: 15 | n_img = len(os.listdir(one_folder)) 16 | letter = one_folder.split('/')[-1] 17 | if letter == 'cat': 18 | labels = np.append(labels, n_img * [0]) 19 | else: 20 | labels = np.append(labels, n_img * [1]) 21 | 22 | # shuffle 23 | temp = np.array([images, labels]) 24 | temp = temp.transpose() 25 | np.random.shuffle(temp) 26 | image_list = list(temp[:, 0]) 27 | label_list = list(temp[:, 1]) 28 | label_list = [int(float(i)) for i in label_list] 29 | print(type(image_list)) 30 | 31 | return image_list, label_list 32 | 33 | 34 | from vgg_preprocess import preprocess_for_train 35 | # 图像预处理模块vgg_preprocess:为了保持与vggnet训练时图像预处理一样的方式,对本案例中的图像进行预处理 36 | 37 | # vggnet模型的图片大小是224*224,alexnet模型图片大小是227*227 38 | img_width = 224 39 | img_height = 224 40 | 41 | def get_batch(image_list, label_list, img_width, img_height, batch_size, capacity): # 通过读取列表批量载入图形和标签 42 | # capacity参数:内存中存储的最大数据容量,可以根据自己的硬件配置来指定 43 | # image = tf.cast(image_list, tf.string) 44 | image = tf.as_string(image_list, tf.string) 45 | label = tf.cast(label_list, tf.int32) 46 | input_queue = tf.train.slice_input_producer([image, label]) 47 | label = input_queue[1] 48 | image_contents = tf.read_file(input_queue[0]) 49 | 50 | image = tf.image.decode_jpeg(image_contents, channels=3) 51 | image = preprocess_for_train(image, 224, 224) 52 | image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, num_threads=64, capacity=capacity) 53 | label_batch = tf.reshape(label_batch, [batch_size]) 54 | 55 | return image_batch, label_batch 56 | 57 | # 对标签形式进行重构,转换成独热编码 58 | def onehot(labels): 59 | n_sample = len(labels) 60 | n_class = max(labels) + 1 61 | onehot_labels = np.zeros((n_sample, n_class)) 62 | onehot_labels[np.arange(n_sample), labels] = 1 63 | return onehot_labels 64 | 65 | -------------------------------------------------------------------------------- /total_program.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | import numpy as np 4 | from time import time 5 | import VGG16_model as model 6 | import input_data_func as idf 7 | 8 | 9 | #-------------------------------------迭代训练----------------------------------------------- 10 | startTime = time() 11 | batch_size = 32 # 每批处理的样本数量 12 | capacity = 256 # 内存中存储的最大数据容量 13 | means = [123.68, 116.779, 103.939] # vgg训练时图像预处理所减均值(RGB三通道) 14 | 15 | xs, ys = idf.get_file('./data/train/*') 16 | 17 | image_batch, label_batch = idf.get_batch(xs, ys, 224, 224, batch_size, capacity) # 通过调用get_batch函数载入图像和标签 18 | 19 | x = tf.placeholder(tf.float32, [None, 224, 224, 3]) 20 | y = tf.placeholder(tf.int32, [None, 2]) 21 | 22 | # 微调finetuining 23 | vgg = model.vgg16(x) 24 | fc8_finetuining = vgg.probs # 即softmax(fc8) 25 | 26 | # 定义损失函数和优化器 27 | loss_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=fc8_finetuining, labels=y)) 28 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss_func) 29 | 30 | # 启动会话,进行训练 31 | sess = tf.Session() 32 | sess.run(tf.global_variables_initializer()) 33 | vgg.load_weights('./vgg16/vgg16_weights.npz', sess) # 读取已经训练好vgg模型相应的权重参数,将权重载入以实现复用 34 | saver = tf.train.Saver() 35 | 36 | # 启动线程 37 | coord = tf.train.Coordinator() # 使用协调器coordinator来管理线程 38 | threads = tf.train.start_queue_runners(coord=coord, sess=sess) 39 | 40 | epoch_start_time = time() 41 | 42 | # 迭代训练 43 | for i in range(1000): 44 | images, labels = sess.run([image_batch, label_batch]) 45 | labels = idf.onehot(labels) # 用one-hot形式对标签进行编码 46 | 47 | sess.run(optimizer, feed_dict={x: images, y: labels}) 48 | loss = sess.run(loss_func, feed_dict={x: images, y: labels}) 49 | print('当前损失值为:%f' % loss) 50 | 51 | epoch_end_time = time() 52 | print('当前轮次耗时:', (epoch_end_time - epoch_start_time)) 53 | epoch_start_time = epoch_end_time 54 | 55 | if (i+1) % 500 ==0: 56 | saver.save(sess, os.path.join('./model/', 'epoch {:06d}.ckpt'.format(i))) 57 | print('--------------------%d轮次完成-------------------------' % i) 58 | 59 | # 模型保存 60 | saver.save(sess, './model/') 61 | print('-----------------优化完成!------------------') 62 | 63 | # 输出优化耗费时间 64 | duration = time() - startTime 65 | print('------------------训练完成!总耗时:{:.2f}--------------------'.format(duration)) 66 | 67 | # 关闭线程 68 | coord.request_stop() # 通知其他线程关闭 69 | coord.join(threads) # join操作等待其他线程结束,其他所有线程关闭后,这一函数才能返回 70 | sess.close() -------------------------------------------------------------------------------- /main_program.py: -------------------------------------------------------------------------------- 1 | import os 2 | import tensorflow as tf 3 | import numpy as np 4 | from time import time 5 | import VGG16_model as model 6 | from vgg_preprocess import preprocess_for_train 7 | # 图像预处理模块vgg_preprocess:为了保持与Vggnet训练时图像预处理一样的方式,对本案例中的图像进行预处理 8 | # from input_data_func import * 9 | 10 | def get_file(file_dir): 11 | images = [] 12 | temp = [] 13 | labels = [] 14 | for root, sub_folders, files in os.walk(file_dir): 15 | for name in files: 16 | images.append(os.path.join(root, name)) 17 | for name in sub_folders: 18 | temp.append(os.path.join(root, name)) 19 | 20 | for one_folder in temp: 21 | n_img = len(os.listdir(one_folder)) 22 | letter = one_folder.split('/')[-1] 23 | if letter == 'cat': 24 | labels = np.append(labels, n_img * [0]) 25 | else: 26 | labels = np.append(labels, n_img * [1]) 27 | 28 | # shuffle 29 | temp = np.array([images, labels]) 30 | temp = temp.transpose() 31 | np.random.shuffle(temp) 32 | image_list = list(temp[:, 0]) 33 | label_list = list(temp[:, 1]) 34 | label_list = [int(float(i)) for i in label_list] 35 | 36 | return image_list, label_list 37 | 38 | 39 | 40 | 41 | # vggnet模型的图片大小是224*224,alexnet模型图片大小是227*227 42 | img_width = 224 43 | img_height = 224 44 | 45 | def get_batch(image_list, label_list, img_width, img_height, batch_size, capacity): # 通过读取列表批量载入图形和标签 46 | # capacity参数:内存中存储的最大数据容量,可以根据自己的硬件配置来指定 47 | image = tf.cast(image_list, tf.string) 48 | label = tf.cast(label_list, tf.int32) 49 | input_queue = tf.train.slice_input_producer([image, label]) 50 | label = input_queue[1] 51 | image_contents = tf.read_file(input_queue[0]) 52 | 53 | image = tf.image.decode_jpeg(image_contents, channels=3) 54 | image = preprocess_for_train(image, 224, 224) 55 | image_batch, label_batch = tf.train.batch([image, label], batch_size=batch_size, num_threads=64, capacity=capacity) 56 | label_batch = tf.reshape(label_batch, [batch_size]) 57 | 58 | return image_batch, label_batch 59 | 60 | # 对标签形式进行重构,转换成独热编码 61 | def onehot(labels): 62 | n_sample = len(labels) 63 | n_class = max(labels) + 1 64 | onehot_labels = np.zeros((n_sample, n_class)) 65 | onehot_labels[np.arange(n_sample), labels] = 1 66 | return onehot_labels 67 | 68 | startTime = time() 69 | batch_size = 32 # 每批处理的样本数量 70 | capacity = 256 # 内存中存储的最大数据容量 71 | means = [123.68, 116.779, 103.939] # vgg训练时图像预处理所减均值(RGB三通道) 72 | 73 | xs, ys = get_file('data/train/') 74 | # print(xs) 75 | # print(ys) 76 | image_batch, label_batch = get_batch(xs, ys, 224, 224, batch_size, capacity) # 通过调用get_batch函数载入图像和标签 77 | # print(image_batch) 78 | # print(label_batch) 79 | 80 | x = tf.placeholder(tf.float32, [None, 224, 224, 3]) 81 | y = tf.placeholder(tf.float32, [None, 2]) 82 | 83 | # 微调finetuining 84 | vgg = model.Vgg16(x) 85 | fc8_finetuining = vgg.probs # 即softmax(fc8) 86 | 87 | # 定义损失函数和优化器 88 | loss_func = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=fc8_finetuining, labels=y)) 89 | # correct_prediction = tf.equal(tf.cast(tf.argmax(y, 1), tf.float32), tf.cast(y, tf.float32)) 90 | # acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 91 | optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss_func) 92 | 93 | # 启动会话,进行训练 94 | sess = tf.Session() 95 | sess.run(tf.global_variables_initializer()) 96 | vgg.load_weights('./vgg16/vgg16_weights.npz', sess) # 读取已经训练好vgg模型相应的权重参数,将权重载入以实现复用 97 | saver = tf.train.Saver() 98 | 99 | # 启动线程 100 | coord = tf.train.Coordinator() # 使用协调器coordinator来管理线程 101 | threads = tf.train.start_queue_runners(coord=coord, sess=sess) 102 | 103 | epoch_start_time = time() 104 | 105 | # 迭代训练 106 | for i in range(1000): 107 | images, labels = sess.run([image_batch, label_batch]) 108 | labels = onehot(labels) # 用one-hot形式对标签进行编码 109 | sess.run(optimizer, feed_dict={x: images, y: labels}) 110 | loss = sess.run(loss_func, feed_dict={x: images, y: labels}) 111 | print('当前损失值为:%f' % loss) 112 | 113 | epoch_end_time = time() 114 | print('当前轮次耗时:', (epoch_end_time - epoch_start_time)) 115 | epoch_start_time = epoch_end_time 116 | 117 | if (i+1) % 50 ==0: 118 | saver.save(sess, os.path.join('model/', 'epoch {:06d}.ckpt'.format(i))) 119 | print('--------------------%d轮次完成-------------------------' % i) 120 | 121 | # 模型保存 122 | saver.save(sess, 'model/') 123 | print('-----------------优化完成!------------------') 124 | 125 | # 输出优化耗费时间 126 | duration = time() - startTime 127 | print('------------------训练完成!总耗时:{:.2f}--------------------'.format(duration)) 128 | 129 | # 关闭线程 130 | coord.request_stop() # 通知其他线程关闭 131 | coord.join(threads) # join操作等待其他线程结束,其他所有线程关闭后,这一函数才能返回 -------------------------------------------------------------------------------- /VGG16_model.py: -------------------------------------------------------------------------------- 1 | 2 | """定义VGG-16模型类:VGG-16的tensorflow实现""" 3 | import tensorflow as tf 4 | import numpy as np 5 | 6 | """ 7 | 微调VGG16模型:全连接层的神经元个数,trainable参数 8 | 1. 预训练的VGG是在ImageNet数据集上进行的,对1000个类别进行判定,若希望利用已训练模型用于其他分类任务, 9 | 需要修改最后的全连接层(猫狗大战为2分类,所以改为2层);同时该层参数需要训练,trainable=true 10 | 2. 在进行微调对模型进行重新训练时,对于部分不需要训练的层可以通过设置trainable=False,来确保其在训练 11 | 过程中不会被修改权值 12 | """ 13 | 14 | class Vgg16: 15 | # 初始化 16 | def __init__(self, imgs): 17 | self.parameters = [] # 在类的初始化时加入全局列表,将所需共享的参数加载进来 18 | self.imgs = imgs 19 | self.convlayers() # 模型定义 20 | self.fc_layers() # 模型定义 21 | self.probs = tf.nn.softmax(self.fc8) # 模型输出:输出属于各个类别的概率值 22 | 23 | def saver(self): # 对复用类的定义没有影响 24 | return tf.train.Saver() 25 | 26 | # 定义池化层方法:没有相应参数(不涉及微调与否的事) 27 | def maxpool(self, name, input_data): 28 | out = tf.nn.max_pool(input_data, [1, 2, 2, 1], [1, 2, 2, 1], padding='SAME', name=name) 29 | return out 30 | 31 | # 定义卷积方法 32 | def conv(self, name, input_data, out_channel, trainable=False): # 设置trainable为False的相应参数不参加训练 33 | in_channel = input_data.get_shape()[-1] 34 | with tf.variable_scope(name): 35 | kernel = tf.get_variable('weights', [3, 3, in_channel, out_channel], dtype=tf.float32, trainable=False) 36 | biases = tf.get_variable('biases', [out_channel], dtype=tf.float32 ,trainable=False) 37 | conv_res = tf.nn.conv2d(input_data, kernel, [1, 1, 1, 1], padding='SAME') 38 | res = tf.nn.bias_add(conv_res, biases) 39 | out = tf.nn.relu(res, name=name) 40 | self.parameters += [kernel, biases] # 将卷积层定义的参数(kernel,biases)加入全局列表 41 | return out 42 | 43 | # 定义全连接层 44 | def fc(self, name, input_data, out_channel, trainable=True): # trainable参数设为true,表示全连接层参数需要参与训练 45 | shape = input_data.get_shape().as_list() 46 | if len(shape) == 4: 47 | size = shape[-1] * shape[-2] * shape[-3] 48 | else: 49 | size = shape[1] 50 | input_data_flat = tf.reshape(input_data, [-1, size]) 51 | with tf.variable_scope(name): 52 | weights = tf.get_variable(name='weights', shape=[size, out_channel], dtype=tf.float32, trainable=trainable) 53 | biases = tf.get_variable(name='biases', shape=[out_channel], dtype=tf.float32, trainable=trainable) 54 | res = tf.matmul(input_data_flat, weights) 55 | out = tf.nn.relu(tf.nn.bias_add(res, biases)) 56 | self.parameters += [weights, biases] # 将全连接层定义的参数(weights,biases)加入全局参数列表 57 | return out 58 | 59 | # 堆叠起来的卷积层和池化层结构:CNN层的所有参数都不需要调整,trainable设为False 60 | def convlayers(self): 61 | # conv1 62 | self.conv1_1 = self.conv('conv1re_1', self.imgs, 64, trainable=False) # self.imgs原始数据输入 63 | self.conv1_2 = self.conv('conv1_2', self.conv1_1, 64, trainable=False) 64 | self.pool1 = self.maxpool('poolre1', self.conv1_2) 65 | 66 | # conv2 67 | self.conv2_1 = self.conv('conv2_1', self.pool1, 128, trainable=False) 68 | self.conv2_2 = self.conv('convwe2_2', self.conv2_1, 128, trainable=False) 69 | self.pool2 = self.maxpool('pool2', self.conv2_2) 70 | 71 | # conv3 72 | self.conv3_1 = self.conv('conv3_1', self.pool2, 256, trainable=False) 73 | self.conv3_2 = self.conv('convrwe3_2', self.conv3_1, 256, trainable=False) 74 | self.conv3_3 = self.conv('convrew3_3', self.conv3_2, 256, trainable=False) 75 | self.pool3 = self.maxpool('poolre3', self.conv3_3) 76 | 77 | # conv4 78 | self.conv4_1 = self.conv('conv4_1', self.pool3, 512, trainable=False) 79 | self.conv4_2 = self.conv('convrwe4_2', self.conv4_1, 512, trainable=False) 80 | self.conv4_3 = self.conv('conv4rwe_3', self.conv4_2, 512, trainable=False) 81 | self.pool4 = self.maxpool('pool4', self.conv4_3) 82 | 83 | # conv5 84 | self.conv5_1 = self.conv('conv5_1', self.pool4, 512, trainable=False) 85 | self.conv5_2 = self.conv('convrwe5_2', self.conv5_1, 512, trainable=False) 86 | self.conv5_3 = self.conv('conv5_3', self.conv5_2, 512, trainable=False) 87 | self.pool5 = self.maxpool('poolrwe5', self.conv5_3) 88 | 89 | # 堆叠起来的全连接层结构 90 | def fc_layers(self): 91 | self.fc6 = self.fc('fc1', self.pool5, 4096, trainable=False) 92 | self.fc7 = self.fc('fc2', self.fc6, 4096, trainable=False) 93 | self.fc8 = self.fc('fc3', self.fc7, 2, trainable=True) # 输出类别的个数(猫狗大战案例输出类别是2) 94 | # fc8正是我们案例中需要训练的,因此trainable设为True 95 | 96 | # 复用已经训练好的权重参数,通过load方法将数据以字典的形式读入 97 | def load_weights(self, weight_file, sess): 98 | weights = np.load(weight_file) 99 | print(weights) 100 | keys = sorted(weights.keys()) 101 | print(keys) 102 | for i,k in enumerate(keys): # enumerate()函数用于将一个可遍历的数据对象(如列表、元组或字符串)组合为一个索引序列,同时列出数据和数据下标,一般用在 for 循环当中 103 | if i not in [30, 31]: # 剔除不需要载入的层(fc8和fc8的输出) 104 | sess.run(self.parameters[i].assign(weights[k])) 105 | print('------------------Weights Loaded!----------------------') -------------------------------------------------------------------------------- /vgg_preprocess.py: -------------------------------------------------------------------------------- 1 | # Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================== 15 | """Provides utilities to preprocess images. 16 | 17 | The preprocessing steps for VGG were introduced in the following technical 18 | report: 19 | 20 | Very Deep Convolutional Networks For Large-Scale Image Recognition 21 | Karen Simonyan and Andrew Zisserman 22 | arXiv technical report, 2015 23 | PDF: http://arxiv.org/pdf/1409.1556.pdf 24 | ILSVRC 2014 Slides: http://www.robots.ox.ac.uk/~karen/pdf/ILSVRC_2014.pdf 25 | CC-BY-4.0 26 | 27 | More information can be obtained from the VGG website: 28 | www.robots.ox.ac.uk/~vgg/research/very_deep/ 29 | """ 30 | 31 | from __future__ import absolute_import 32 | from __future__ import division 33 | from __future__ import print_function 34 | 35 | import tensorflow as tf 36 | 37 | slim = tf.contrib.slim 38 | 39 | _R_MEAN = 123.68 40 | _G_MEAN = 116.78 41 | _B_MEAN = 103.94 42 | 43 | _RESIZE_SIDE_MIN = 256 44 | _RESIZE_SIDE_MAX = 512 45 | 46 | 47 | def _crop(image, offset_height, offset_width, crop_height, crop_width): 48 | """Crops the given image using the provided offsets and sizes. 49 | 50 | Note that the method doesn't assume we know the input image size but it does 51 | assume we know the input image rank. 52 | 53 | Args: 54 | image: an image of shape [height, width, channels]. 55 | offset_height: a scalar tensor indicating the height offset. 56 | offset_width: a scalar tensor indicating the width offset. 57 | crop_height: the height of the cropped image. 58 | crop_width: the width of the cropped image. 59 | 60 | Returns: 61 | the cropped (and resized) image. 62 | 63 | Raises: 64 | InvalidArgumentError: if the rank is not 3 or if the image dimensions are 65 | less than the crop size. 66 | """ 67 | original_shape = tf.shape(image) 68 | 69 | rank_assertion = tf.Assert( 70 | tf.equal(tf.rank(image), 3), 71 | ['Rank of image must be equal to 3.']) 72 | with tf.control_dependencies([rank_assertion]): 73 | cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]]) 74 | 75 | size_assertion = tf.Assert( 76 | tf.logical_and( 77 | tf.greater_equal(original_shape[0], crop_height), 78 | tf.greater_equal(original_shape[1], crop_width)), 79 | ['Crop size greater than the image size.']) 80 | 81 | offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0])) 82 | 83 | # Use tf.slice instead of crop_to_bounding box as it accepts tensors to 84 | # define the crop size. 85 | with tf.control_dependencies([size_assertion]): 86 | image = tf.slice(image, offsets, cropped_shape) 87 | return tf.reshape(image, cropped_shape) 88 | 89 | 90 | def _random_crop(image_list, crop_height, crop_width): 91 | """Crops the given list of images. 92 | 93 | The function applies the same crop to each image in the list. This can be 94 | effectively applied when there are multiple image inputs of the same 95 | dimension such as: 96 | 97 | image, depths, normals = _random_crop([image, depths, normals], 120, 150) 98 | 99 | Args: 100 | image_list: a list of image tensors of the same dimension but possibly 101 | varying channel. 102 | crop_height: the new height. 103 | crop_width: the new width. 104 | 105 | Returns: 106 | the image_list with cropped images. 107 | 108 | Raises: 109 | ValueError: if there are multiple image inputs provided with different size 110 | or the images are smaller than the crop dimensions. 111 | """ 112 | if not image_list: 113 | raise ValueError('Empty image_list.') 114 | 115 | # Compute the rank assertions. 116 | rank_assertions = [] 117 | for i in range(len(image_list)): 118 | image_rank = tf.rank(image_list[i]) 119 | rank_assert = tf.Assert( 120 | tf.equal(image_rank, 3), 121 | ['Wrong rank for tensor %s [expected] [actual]', 122 | image_list[i].name, 3, image_rank]) 123 | rank_assertions.append(rank_assert) 124 | 125 | with tf.control_dependencies([rank_assertions[0]]): 126 | image_shape = tf.shape(image_list[0]) 127 | image_height = image_shape[0] 128 | image_width = image_shape[1] 129 | crop_size_assert = tf.Assert( 130 | tf.logical_and( 131 | tf.greater_equal(image_height, crop_height), 132 | tf.greater_equal(image_width, crop_width)), 133 | ['Crop size greater than the image size.']) 134 | 135 | asserts = [rank_assertions[0], crop_size_assert] 136 | 137 | for i in range(1, len(image_list)): 138 | image = image_list[i] 139 | asserts.append(rank_assertions[i]) 140 | with tf.control_dependencies([rank_assertions[i]]): 141 | shape = tf.shape(image) 142 | height = shape[0] 143 | width = shape[1] 144 | 145 | height_assert = tf.Assert( 146 | tf.equal(height, image_height), 147 | ['Wrong height for tensor %s [expected][actual]', 148 | image.name, height, image_height]) 149 | width_assert = tf.Assert( 150 | tf.equal(width, image_width), 151 | ['Wrong width for tensor %s [expected][actual]', 152 | image.name, width, image_width]) 153 | asserts.extend([height_assert, width_assert]) 154 | 155 | # Create a random bounding box. 156 | # 157 | # Use tf.random_uniform and not numpy.random.rand as doing the former would 158 | # generate random numbers at graph eval time, unlike the latter which 159 | # generates random numbers at graph definition time. 160 | with tf.control_dependencies(asserts): 161 | max_offset_height = tf.reshape(image_height - crop_height + 1, []) 162 | with tf.control_dependencies(asserts): 163 | max_offset_width = tf.reshape(image_width - crop_width + 1, []) 164 | offset_height = tf.random_uniform( 165 | [], maxval=max_offset_height, dtype=tf.int32) 166 | offset_width = tf.random_uniform( 167 | [], maxval=max_offset_width, dtype=tf.int32) 168 | 169 | return [_crop(image, offset_height, offset_width, 170 | crop_height, crop_width) for image in image_list] 171 | 172 | 173 | def _central_crop(image_list, crop_height, crop_width): 174 | """Performs central crops of the given image list. 175 | 176 | Args: 177 | image_list: a list of image tensors of the same dimension but possibly 178 | varying channel. 179 | crop_height: the height of the image following the crop. 180 | crop_width: the width of the image following the crop. 181 | 182 | Returns: 183 | the list of cropped images. 184 | """ 185 | outputs = [] 186 | for image in image_list: 187 | image_height = tf.shape(image)[0] 188 | image_width = tf.shape(image)[1] 189 | 190 | offset_height = (image_height - crop_height) / 2 191 | offset_width = (image_width - crop_width) / 2 192 | 193 | outputs.append(_crop(image, offset_height, offset_width, 194 | crop_height, crop_width)) 195 | return outputs 196 | 197 | 198 | def _mean_image_subtraction(image, means): 199 | """Subtracts the given means from each image channel. 200 | 201 | For example: 202 | means = [123.68, 116.779, 103.939] 203 | image = _mean_image_subtraction(image, means) 204 | 205 | Note that the rank of `image` must be known. 206 | 207 | Args: 208 | image: a tensor of size [height, width, C]. 209 | means: a C-vector of values to subtract from each channel. 210 | 211 | Returns: 212 | the centered image. 213 | 214 | Raises: 215 | ValueError: If the rank of `image` is unknown, if `image` has a rank other 216 | than three or if the number of channels in `image` doesn't match the 217 | number of values in `means`. 218 | """ 219 | if image.get_shape().ndims != 3: 220 | raise ValueError('Input must be of size [height, width, C>0]') 221 | num_channels = image.get_shape().as_list()[-1] 222 | if len(means) != num_channels: 223 | raise ValueError('len(means) must match the number of channels') 224 | 225 | channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image) 226 | for i in range(num_channels): 227 | channels[i] -= means[i] 228 | return tf.concat(axis=2, values=channels) 229 | 230 | 231 | def _smallest_size_at_least(height, width, smallest_side): 232 | """Computes new shape with the smallest side equal to `smallest_side`. 233 | 234 | Computes new shape with the smallest side equal to `smallest_side` while 235 | preserving the original aspect ratio. 236 | 237 | Args: 238 | height: an int32 scalar tensor indicating the current height. 239 | width: an int32 scalar tensor indicating the current width. 240 | smallest_side: A python integer or scalar `Tensor` indicating the size of 241 | the smallest side after resize. 242 | 243 | Returns: 244 | new_height: an int32 scalar tensor indicating the new height. 245 | new_width: and int32 scalar tensor indicating the new width. 246 | """ 247 | smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32) 248 | 249 | height = tf.to_float(height) 250 | width = tf.to_float(width) 251 | smallest_side = tf.to_float(smallest_side) 252 | 253 | scale = tf.cond(tf.greater(height, width), 254 | lambda: smallest_side / width, 255 | lambda: smallest_side / height) 256 | new_height = tf.to_int32(height * scale) 257 | new_width = tf.to_int32(width * scale) 258 | return new_height, new_width 259 | 260 | 261 | def _aspect_preserving_resize(image, smallest_side): 262 | """Resize images preserving the original aspect ratio. 263 | 264 | Args: 265 | image: A 3-D image `Tensor`. 266 | smallest_side: A python integer or scalar `Tensor` indicating the size of 267 | the smallest side after resize. 268 | 269 | Returns: 270 | resized_image: A 3-D tensor containing the resized image. 271 | """ 272 | smallest_side = tf.convert_to_tensor(smallest_side, dtype=tf.int32) 273 | 274 | shape = tf.shape(image) 275 | height = shape[0] 276 | width = shape[1] 277 | new_height, new_width = _smallest_size_at_least(height, width, smallest_side) 278 | image = tf.expand_dims(image, 0) 279 | resized_image = tf.image.resize_bilinear(image, [new_height, new_width], 280 | align_corners=False) 281 | resized_image = tf.squeeze(resized_image) 282 | resized_image.set_shape([None, None, 3]) 283 | return resized_image 284 | 285 | 286 | def preprocess_for_train(image, 287 | output_height, 288 | output_width, 289 | resize_side_min=_RESIZE_SIDE_MIN, 290 | resize_side_max=_RESIZE_SIDE_MAX): 291 | """Preprocesses the given image for training. 292 | 293 | Note that the actual resizing scale is sampled from 294 | [`resize_size_min`, `resize_size_max`]. 295 | 296 | Args: 297 | image: A `Tensor` representing an image of arbitrary size. 298 | output_height: The height of the image after preprocessing. 299 | output_width: The width of the image after preprocessing. 300 | resize_side_min: The lower bound for the smallest side of the image for 301 | aspect-preserving resizing. 302 | resize_side_max: The upper bound for the smallest side of the image for 303 | aspect-preserving resizing. 304 | 305 | Returns: 306 | A preprocessed image. 307 | """ 308 | resize_side = tf.random_uniform( 309 | [], minval=resize_side_min, maxval=resize_side_max+1, dtype=tf.int32) 310 | 311 | image = _aspect_preserving_resize(image, resize_side) 312 | image = _random_crop([image], output_height, output_width)[0] 313 | image.set_shape([output_height, output_width, 3]) 314 | image = tf.to_float(image) 315 | image = tf.image.random_flip_left_right(image) 316 | return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN]) 317 | 318 | 319 | def preprocess_for_eval(image, output_height, output_width, resize_side): 320 | """Preprocesses the given image for evaluation. 321 | 322 | Args: 323 | image: A `Tensor` representing an image of arbitrary size. 324 | output_height: The height of the image after preprocessing. 325 | output_width: The width of the image after preprocessing. 326 | resize_side: The smallest side of the image for aspect-preserving resizing. 327 | 328 | Returns: 329 | A preprocessed image. 330 | """ 331 | image = _aspect_preserving_resize(image, resize_side) 332 | image = _central_crop([image], output_height, output_width)[0] 333 | image.set_shape([output_height, output_width, 3]) 334 | image = tf.to_float(image) 335 | return _mean_image_subtraction(image, [_R_MEAN, _G_MEAN, _B_MEAN]) 336 | 337 | 338 | def preprocess_image(image, output_height, output_width, is_training=False, 339 | resize_side_min=_RESIZE_SIDE_MIN, 340 | resize_side_max=_RESIZE_SIDE_MAX): 341 | """Preprocesses the given image. 342 | 343 | Args: 344 | image: A `Tensor` representing an image of arbitrary size. 345 | output_height: The height of the image after preprocessing. 346 | output_width: The width of the image after preprocessing. 347 | is_training: `True` if we're preprocessing the image for training and 348 | `False` otherwise. 349 | resize_side_min: The lower bound for the smallest side of the image for 350 | aspect-preserving resizing. If `is_training` is `False`, then this value 351 | is used for rescaling. 352 | resize_side_max: The upper bound for the smallest side of the image for 353 | aspect-preserving resizing. If `is_training` is `False`, this value is 354 | ignored. Otherwise, the resize side is sampled from 355 | [resize_size_min, resize_size_max]. 356 | 357 | Returns: 358 | A preprocessed image. 359 | """ 360 | if is_training: 361 | return preprocess_for_train(image, output_height, output_width, 362 | resize_side_min, resize_side_max) 363 | else: 364 | return preprocess_for_eval(image, output_height, output_width, 365 | resize_side_min) 366 | -------------------------------------------------------------------------------- /imagenet_classes.py: -------------------------------------------------------------------------------- 1 | class_names = '''tench, Tinca tinca 2 | goldfish, Carassius auratus 3 | great white shark, white shark, man-eater, man-eating shark, Carcharodon carcharias 4 | tiger shark, Galeocerdo cuvieri 5 | hammerhead, hammerhead shark 6 | electric ray, crampfish, numbfish, torpedo 7 | stingray 8 | cock 9 | hen 10 | ostrich, Struthio camelus 11 | brambling, Fringilla montifringilla 12 | goldfinch, Carduelis carduelis 13 | house finch, linnet, Carpodacus mexicanus 14 | junco, snowbird 15 | indigo bunting, indigo finch, indigo bird, Passerina cyanea 16 | robin, American robin, Turdus migratorius 17 | bulbul 18 | jay 19 | magpie 20 | chickadee 21 | water ouzel, dipper 22 | kite 23 | bald eagle, American eagle, Haliaeetus leucocephalus 24 | vulture 25 | great grey owl, great gray owl, Strix nebulosa 26 | European fire salamander, Salamandra salamandra 27 | common newt, Triturus vulgaris 28 | eft 29 | spotted salamander, Ambystoma maculatum 30 | axolotl, mud puppy, Ambystoma mexicanum 31 | bullfrog, Rana catesbeiana 32 | tree frog, tree-frog 33 | tailed frog, bell toad, ribbed toad, tailed toad, Ascaphus trui 34 | loggerhead, loggerhead turtle, Caretta caretta 35 | leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea 36 | mud turtle 37 | terrapin 38 | box turtle, box tortoise 39 | banded gecko 40 | common iguana, iguana, Iguana iguana 41 | American chameleon, anole, Anolis carolinensis 42 | whiptail, whiptail lizard 43 | agama 44 | frilled lizard, Chlamydosaurus kingi 45 | alligator lizard 46 | Gila monster, Heloderma suspectum 47 | green lizard, Lacerta viridis 48 | African chameleon, Chamaeleo chamaeleon 49 | Komodo dragon, Komodo lizard, dragon lizard, giant lizard, Varanus komodoensis 50 | African crocodile, Nile crocodile, Crocodylus niloticus 51 | American alligator, Alligator mississipiensis 52 | triceratops 53 | thunder snake, worm snake, Carphophis amoenus 54 | ringneck snake, ring-necked snake, ring snake 55 | hognose snake, puff adder, sand viper 56 | green snake, grass snake 57 | king snake, kingsnake 58 | garter snake, grass snake 59 | water snake 60 | vine snake 61 | night snake, Hypsiglena torquata 62 | boa constrictor, Constrictor constrictor 63 | rock python, rock snake, Python sebae 64 | Indian cobra, Naja naja 65 | green mamba 66 | sea snake 67 | horned viper, cerastes, sand viper, horned asp, Cerastes cornutus 68 | diamondback, diamondback rattlesnake, Crotalus adamanteus 69 | sidewinder, horned rattlesnake, Crotalus cerastes 70 | trilobite 71 | harvestman, daddy longlegs, Phalangium opilio 72 | scorpion 73 | black and gold garden spider, Argiope aurantia 74 | barn spider, Araneus cavaticus 75 | garden spider, Aranea diademata 76 | black widow, Latrodectus mactans 77 | tarantula 78 | wolf spider, hunting spider 79 | tick 80 | centipede 81 | black grouse 82 | ptarmigan 83 | ruffed grouse, partridge, Bonasa umbellus 84 | prairie chicken, prairie grouse, prairie fowl 85 | peacock 86 | quail 87 | partridge 88 | African grey, African gray, Psittacus erithacus 89 | macaw 90 | sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita 91 | lorikeet 92 | coucal 93 | bee eater 94 | hornbill 95 | hummingbird 96 | jacamar 97 | toucan 98 | drake 99 | red-breasted merganser, Mergus serrator 100 | goose 101 | black swan, Cygnus atratus 102 | tusker 103 | echidna, spiny anteater, anteater 104 | platypus, duckbill, duckbilled platypus, duck-billed platypus, Ornithorhynchus anatinus 105 | wallaby, brush kangaroo 106 | koala, koala bear, kangaroo bear, native bear, Phascolarctos cinereus 107 | wombat 108 | jellyfish 109 | sea anemone, anemone 110 | brain coral 111 | flatworm, platyhelminth 112 | nematode, nematode worm, roundworm 113 | conch 114 | snail 115 | slug 116 | sea slug, nudibranch 117 | chiton, coat-of-mail shell, sea cradle, polyplacophore 118 | chambered nautilus, pearly nautilus, nautilus 119 | Dungeness crab, Cancer magister 120 | rock crab, Cancer irroratus 121 | fiddler crab 122 | king crab, Alaska crab, Alaskan king crab, Alaska king crab, Paralithodes camtschatica 123 | American lobster, Northern lobster, Maine lobster, Homarus americanus 124 | spiny lobster, langouste, rock lobster, crawfish, crayfish, sea crawfish 125 | crayfish, crawfish, crawdad, crawdaddy 126 | hermit crab 127 | isopod 128 | white stork, Ciconia ciconia 129 | black stork, Ciconia nigra 130 | spoonbill 131 | flamingo 132 | little blue heron, Egretta caerulea 133 | American egret, great white heron, Egretta albus 134 | bittern 135 | crane 136 | limpkin, Aramus pictus 137 | European gallinule, Porphyrio porphyrio 138 | American coot, marsh hen, mud hen, water hen, Fulica americana 139 | bustard 140 | ruddy turnstone, Arenaria interpres 141 | red-backed sandpiper, dunlin, Erolia alpina 142 | redshank, Tringa totanus 143 | dowitcher 144 | oystercatcher, oyster catcher 145 | pelican 146 | king penguin, Aptenodytes patagonica 147 | albatross, mollymawk 148 | grey whale, gray whale, devilfish, Eschrichtius gibbosus, Eschrichtius robustus 149 | killer whale, killer, orca, grampus, sea wolf, Orcinus orca 150 | dugong, Dugong dugon 151 | sea lion 152 | Chihuahua 153 | Japanese spaniel 154 | Maltese dog, Maltese terrier, Maltese 155 | Pekinese, Pekingese, Peke 156 | Shih-Tzu 157 | Blenheim spaniel 158 | papillon 159 | toy terrier 160 | Rhodesian ridgeback 161 | Afghan hound, Afghan 162 | basset, basset hound 163 | beagle 164 | bloodhound, sleuthhound 165 | bluetick 166 | black-and-tan coonhound 167 | Walker hound, Walker foxhound 168 | English foxhound 169 | redbone 170 | borzoi, Russian wolfhound 171 | Irish wolfhound 172 | Italian greyhound 173 | whippet 174 | Ibizan hound, Ibizan Podenco 175 | Norwegian elkhound, elkhound 176 | otterhound, otter hound 177 | Saluki, gazelle hound 178 | Scottish deerhound, deerhound 179 | Weimaraner 180 | Staffordshire bullterrier, Staffordshire bull terrier 181 | American Staffordshire terrier, Staffordshire terrier, American pit bull terrier, pit bull terrier 182 | Bedlington terrier 183 | Border terrier 184 | Kerry blue terrier 185 | Irish terrier 186 | Norfolk terrier 187 | Norwich terrier 188 | Yorkshire terrier 189 | wire-haired fox terrier 190 | Lakeland terrier 191 | Sealyham terrier, Sealyham 192 | Airedale, Airedale terrier 193 | cairn, cairn terrier 194 | Australian terrier 195 | Dandie Dinmont, Dandie Dinmont terrier 196 | Boston bull, Boston terrier 197 | miniature schnauzer 198 | giant schnauzer 199 | standard schnauzer 200 | Scotch terrier, Scottish terrier, Scottie 201 | Tibetan terrier, chrysanthemum dog 202 | silky terrier, Sydney silky 203 | soft-coated wheaten terrier 204 | West Highland white terrier 205 | Lhasa, Lhasa apso 206 | flat-coated retriever 207 | curly-coated retriever 208 | golden retriever 209 | Labrador retriever 210 | Chesapeake Bay retriever 211 | German short-haired pointer 212 | vizsla, Hungarian pointer 213 | English setter 214 | Irish setter, red setter 215 | Gordon setter 216 | Brittany spaniel 217 | clumber, clumber spaniel 218 | English springer, English springer spaniel 219 | Welsh springer spaniel 220 | cocker spaniel, English cocker spaniel, cocker 221 | Sussex spaniel 222 | Irish water spaniel 223 | kuvasz 224 | schipperke 225 | groenendael 226 | malinois 227 | briard 228 | kelpie 229 | komondor 230 | Old English sheepdog, bobtail 231 | Shetland sheepdog, Shetland sheep dog, Shetland 232 | collie 233 | Border collie 234 | Bouvier des Flandres, Bouviers des Flandres 235 | Rottweiler 236 | German shepherd, German shepherd dog, German police dog, alsatian 237 | Doberman, Doberman pinscher 238 | miniature pinscher 239 | Greater Swiss Mountain dog 240 | Bernese mountain dog 241 | Appenzeller 242 | EntleBucher 243 | boxer 244 | bull mastiff 245 | Tibetan mastiff 246 | French bulldog 247 | Great Dane 248 | Saint Bernard, St Bernard 249 | Eskimo dog, husky 250 | malamute, malemute, Alaskan malamute 251 | Siberian husky 252 | dalmatian, coach dog, carriage dog 253 | affenpinscher, monkey pinscher, monkey dog 254 | basenji 255 | pug, pug-dog 256 | Leonberg 257 | Newfoundland, Newfoundland dog 258 | Great Pyrenees 259 | Samoyed, Samoyede 260 | Pomeranian 261 | chow, chow chow 262 | keeshond 263 | Brabancon griffon 264 | Pembroke, Pembroke Welsh corgi 265 | Cardigan, Cardigan Welsh corgi 266 | toy poodle 267 | miniature poodle 268 | standard poodle 269 | Mexican hairless 270 | timber wolf, grey wolf, gray wolf, Canis lupus 271 | white wolf, Arctic wolf, Canis lupus tundrarum 272 | red wolf, maned wolf, Canis rufus, Canis niger 273 | coyote, prairie wolf, brush wolf, Canis latrans 274 | dingo, warrigal, warragal, Canis dingo 275 | dhole, Cuon alpinus 276 | African hunting dog, hyena dog, Cape hunting dog, Lycaon pictus 277 | hyena, hyaena 278 | red fox, Vulpes vulpes 279 | kit fox, Vulpes macrotis 280 | Arctic fox, white fox, Alopex lagopus 281 | grey fox, gray fox, Urocyon cinereoargenteus 282 | tabby, tabby cat 283 | tiger cat 284 | Persian cat 285 | Siamese cat, Siamese 286 | Egyptian cat 287 | cougar, puma, catamount, mountain lion, painter, panther, Felis concolor 288 | lynx, catamount 289 | leopard, Panthera pardus 290 | snow leopard, ounce, Panthera uncia 291 | jaguar, panther, Panthera onca, Felis onca 292 | lion, king of beasts, Panthera leo 293 | tiger, Panthera tigris 294 | cheetah, chetah, Acinonyx jubatus 295 | brown bear, bruin, Ursus arctos 296 | American black bear, black bear, Ursus americanus, Euarctos americanus 297 | ice bear, polar bear, Ursus Maritimus, Thalarctos maritimus 298 | sloth bear, Melursus ursinus, Ursus ursinus 299 | mongoose 300 | meerkat, mierkat 301 | tiger beetle 302 | ladybug, ladybeetle, lady beetle, ladybird, ladybird beetle 303 | ground beetle, carabid beetle 304 | long-horned beetle, longicorn, longicorn beetle 305 | leaf beetle, chrysomelid 306 | dung beetle 307 | rhinoceros beetle 308 | weevil 309 | fly 310 | bee 311 | ant, emmet, pismire 312 | grasshopper, hopper 313 | cricket 314 | walking stick, walkingstick, stick insect 315 | cockroach, roach 316 | mantis, mantid 317 | cicada, cicala 318 | leafhopper 319 | lacewing, lacewing fly 320 | dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk 321 | damselfly 322 | admiral 323 | ringlet, ringlet butterfly 324 | monarch, monarch butterfly, milkweed butterfly, Danaus plexippus 325 | cabbage butterfly 326 | sulphur butterfly, sulfur butterfly 327 | lycaenid, lycaenid butterfly 328 | starfish, sea star 329 | sea urchin 330 | sea cucumber, holothurian 331 | wood rabbit, cottontail, cottontail rabbit 332 | hare 333 | Angora, Angora rabbit 334 | hamster 335 | porcupine, hedgehog 336 | fox squirrel, eastern fox squirrel, Sciurus niger 337 | marmot 338 | beaver 339 | guinea pig, Cavia cobaya 340 | sorrel 341 | zebra 342 | hog, pig, grunter, squealer, Sus scrofa 343 | wild boar, boar, Sus scrofa 344 | warthog 345 | hippopotamus, hippo, river horse, Hippopotamus amphibius 346 | ox 347 | water buffalo, water ox, Asiatic buffalo, Bubalus bubalis 348 | bison 349 | ram, tup 350 | bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis 351 | ibex, Capra ibex 352 | hartebeest 353 | impala, Aepyceros melampus 354 | gazelle 355 | Arabian camel, dromedary, Camelus dromedarius 356 | llama 357 | weasel 358 | mink 359 | polecat, fitch, foulmart, foumart, Mustela putorius 360 | black-footed ferret, ferret, Mustela nigripes 361 | otter 362 | skunk, polecat, wood pussy 363 | badger 364 | armadillo 365 | three-toed sloth, ai, Bradypus tridactylus 366 | orangutan, orang, orangutang, Pongo pygmaeus 367 | gorilla, Gorilla gorilla 368 | chimpanzee, chimp, Pan troglodytes 369 | gibbon, Hylobates lar 370 | siamang, Hylobates syndactylus, Symphalangus syndactylus 371 | guenon, guenon monkey 372 | patas, hussar monkey, Erythrocebus patas 373 | baboon 374 | macaque 375 | langur 376 | colobus, colobus monkey 377 | proboscis monkey, Nasalis larvatus 378 | marmoset 379 | capuchin, ringtail, Cebus capucinus 380 | howler monkey, howler 381 | titi, titi monkey 382 | spider monkey, Ateles geoffroyi 383 | squirrel monkey, Saimiri sciureus 384 | Madagascar cat, ring-tailed lemur, Lemur catta 385 | indri, indris, Indri indri, Indri brevicaudatus 386 | Indian elephant, Elephas maximus 387 | African elephant, Loxodonta africana 388 | lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens 389 | giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca 390 | barracouta, snoek 391 | eel 392 | coho, cohoe, coho salmon, blue jack, silver salmon, Oncorhynchus kisutch 393 | rock beauty, Holocanthus tricolor 394 | anemone fish 395 | sturgeon 396 | gar, garfish, garpike, billfish, Lepisosteus osseus 397 | lionfish 398 | puffer, pufferfish, blowfish, globefish 399 | abacus 400 | abaya 401 | academic gown, academic robe, judge's robe 402 | accordion, piano accordion, squeeze box 403 | acoustic guitar 404 | aircraft carrier, carrier, flattop, attack aircraft carrier 405 | airliner 406 | airship, dirigible 407 | altar 408 | ambulance 409 | amphibian, amphibious vehicle 410 | analog clock 411 | apiary, bee house 412 | apron 413 | ashcan, trash can, garbage can, wastebin, ash bin, ash-bin, ashbin, dustbin, trash barrel, trash bin 414 | assault rifle, assault gun 415 | backpack, back pack, knapsack, packsack, rucksack, haversack 416 | bakery, bakeshop, bakehouse 417 | balance beam, beam 418 | balloon 419 | ballpoint, ballpoint pen, ballpen, Biro 420 | Band Aid 421 | banjo 422 | bannister, banister, balustrade, balusters, handrail 423 | barbell 424 | barber chair 425 | barbershop 426 | barn 427 | barometer 428 | barrel, cask 429 | barrow, garden cart, lawn cart, wheelbarrow 430 | baseball 431 | basketball 432 | bassinet 433 | bassoon 434 | bathing cap, swimming cap 435 | bath towel 436 | bathtub, bathing tub, bath, tub 437 | beach wagon, station wagon, wagon, estate car, beach waggon, station waggon, waggon 438 | beacon, lighthouse, beacon light, pharos 439 | beaker 440 | bearskin, busby, shako 441 | beer bottle 442 | beer glass 443 | bell cote, bell cot 444 | bib 445 | bicycle-built-for-two, tandem bicycle, tandem 446 | bikini, two-piece 447 | binder, ring-binder 448 | binoculars, field glasses, opera glasses 449 | birdhouse 450 | boathouse 451 | bobsled, bobsleigh, bob 452 | bolo tie, bolo, bola tie, bola 453 | bonnet, poke bonnet 454 | bookcase 455 | bookshop, bookstore, bookstall 456 | bottlecap 457 | bow 458 | bow tie, bow-tie, bowtie 459 | brass, memorial tablet, plaque 460 | brassiere, bra, bandeau 461 | breakwater, groin, groyne, mole, bulwark, seawall, jetty 462 | breastplate, aegis, egis 463 | broom 464 | bucket, pail 465 | buckle 466 | bulletproof vest 467 | bullet train, bullet 468 | butcher shop, meat market 469 | cab, hack, taxi, taxicab 470 | caldron, cauldron 471 | candle, taper, wax light 472 | cannon 473 | canoe 474 | can opener, tin opener 475 | cardigan 476 | car mirror 477 | carousel, carrousel, merry-go-round, roundabout, whirligig 478 | carpenter's kit, tool kit 479 | carton 480 | car wheel 481 | cash machine, cash dispenser, automated teller machine, automatic teller machine, automated teller, automatic teller, ATM 482 | cassette 483 | cassette player 484 | castle 485 | catamaran 486 | CD player 487 | cello, violoncello 488 | cellular telephone, cellular phone, cellphone, cell, mobile phone 489 | chain 490 | chainlink fence 491 | chain mail, ring mail, mail, chain armor, chain armour, ring armor, ring armour 492 | chain saw, chainsaw 493 | chest 494 | chiffonier, commode 495 | chime, bell, gong 496 | china cabinet, china closet 497 | Christmas stocking 498 | church, church building 499 | cinema, movie theater, movie theatre, movie house, picture palace 500 | cleaver, meat cleaver, chopper 501 | cliff dwelling 502 | cloak 503 | clog, geta, patten, sabot 504 | cocktail shaker 505 | coffee mug 506 | coffeepot 507 | coil, spiral, volute, whorl, helix 508 | combination lock 509 | computer keyboard, keypad 510 | confectionery, confectionary, candy store 511 | container ship, containership, container vessel 512 | convertible 513 | corkscrew, bottle screw 514 | cornet, horn, trumpet, trump 515 | cowboy boot 516 | cowboy hat, ten-gallon hat 517 | cradle 518 | crane 519 | crash helmet 520 | crate 521 | crib, cot 522 | Crock Pot 523 | croquet ball 524 | crutch 525 | cuirass 526 | dam, dike, dyke 527 | desk 528 | desktop computer 529 | dial telephone, dial phone 530 | diaper, nappy, napkin 531 | digital clock 532 | digital watch 533 | dining table, board 534 | dishrag, dishcloth 535 | dishwasher, dish washer, dishwashing machine 536 | disk brake, disc brake 537 | dock, dockage, docking facility 538 | dogsled, dog sled, dog sleigh 539 | dome 540 | doormat, welcome mat 541 | drilling platform, offshore rig 542 | drum, membranophone, tympan 543 | drumstick 544 | dumbbell 545 | Dutch oven 546 | electric fan, blower 547 | electric guitar 548 | electric locomotive 549 | entertainment center 550 | envelope 551 | espresso maker 552 | face powder 553 | feather boa, boa 554 | file, file cabinet, filing cabinet 555 | fireboat 556 | fire engine, fire truck 557 | fire screen, fireguard 558 | flagpole, flagstaff 559 | flute, transverse flute 560 | folding chair 561 | football helmet 562 | forklift 563 | fountain 564 | fountain pen 565 | four-poster 566 | freight car 567 | French horn, horn 568 | frying pan, frypan, skillet 569 | fur coat 570 | garbage truck, dustcart 571 | gasmask, respirator, gas helmet 572 | gas pump, gasoline pump, petrol pump, island dispenser 573 | goblet 574 | go-kart 575 | golf ball 576 | golfcart, golf cart 577 | gondola 578 | gong, tam-tam 579 | gown 580 | grand piano, grand 581 | greenhouse, nursery, glasshouse 582 | grille, radiator grille 583 | grocery store, grocery, food market, market 584 | guillotine 585 | hair slide 586 | hair spray 587 | half track 588 | hammer 589 | hamper 590 | hand blower, blow dryer, blow drier, hair dryer, hair drier 591 | hand-held computer, hand-held microcomputer 592 | handkerchief, hankie, hanky, hankey 593 | hard disc, hard disk, fixed disk 594 | harmonica, mouth organ, harp, mouth harp 595 | harp 596 | harvester, reaper 597 | hatchet 598 | holster 599 | home theater, home theatre 600 | honeycomb 601 | hook, claw 602 | hoopskirt, crinoline 603 | horizontal bar, high bar 604 | horse cart, horse-cart 605 | hourglass 606 | iPod 607 | iron, smoothing iron 608 | jack-o'-lantern 609 | jean, blue jean, denim 610 | jeep, landrover 611 | jersey, T-shirt, tee shirt 612 | jigsaw puzzle 613 | jinrikisha, ricksha, rickshaw 614 | joystick 615 | kimono 616 | knee pad 617 | knot 618 | lab coat, laboratory coat 619 | ladle 620 | lampshade, lamp shade 621 | laptop, laptop computer 622 | lawn mower, mower 623 | lens cap, lens cover 624 | letter opener, paper knife, paperknife 625 | library 626 | lifeboat 627 | lighter, light, igniter, ignitor 628 | limousine, limo 629 | liner, ocean liner 630 | lipstick, lip rouge 631 | Loafer 632 | lotion 633 | loudspeaker, speaker, speaker unit, loudspeaker system, speaker system 634 | loupe, jeweler's loupe 635 | lumbermill, sawmill 636 | magnetic compass 637 | mailbag, postbag 638 | mailbox, letter box 639 | maillot 640 | maillot, tank suit 641 | manhole cover 642 | maraca 643 | marimba, xylophone 644 | mask 645 | matchstick 646 | maypole 647 | maze, labyrinth 648 | measuring cup 649 | medicine chest, medicine cabinet 650 | megalith, megalithic structure 651 | microphone, mike 652 | microwave, microwave oven 653 | military uniform 654 | milk can 655 | minibus 656 | miniskirt, mini 657 | minivan 658 | missile 659 | mitten 660 | mixing bowl 661 | mobile home, manufactured home 662 | Model T 663 | modem 664 | monastery 665 | monitor 666 | moped 667 | mortar 668 | mortarboard 669 | mosque 670 | mosquito net 671 | motor scooter, scooter 672 | mountain bike, all-terrain bike, off-roader 673 | mountain tent 674 | mouse, computer mouse 675 | mousetrap 676 | moving van 677 | muzzle 678 | nail 679 | neck brace 680 | necklace 681 | nipple 682 | notebook, notebook computer 683 | obelisk 684 | oboe, hautboy, hautbois 685 | ocarina, sweet potato 686 | odometer, hodometer, mileometer, milometer 687 | oil filter 688 | organ, pipe organ 689 | oscilloscope, scope, cathode-ray oscilloscope, CRO 690 | overskirt 691 | oxcart 692 | oxygen mask 693 | packet 694 | paddle, boat paddle 695 | paddlewheel, paddle wheel 696 | padlock 697 | paintbrush 698 | pajama, pyjama, pj's, jammies 699 | palace 700 | panpipe, pandean pipe, syrinx 701 | paper towel 702 | parachute, chute 703 | parallel bars, bars 704 | park bench 705 | parking meter 706 | passenger car, coach, carriage 707 | patio, terrace 708 | pay-phone, pay-station 709 | pedestal, plinth, footstall 710 | pencil box, pencil case 711 | pencil sharpener 712 | perfume, essence 713 | Petri dish 714 | photocopier 715 | pick, plectrum, plectron 716 | pickelhaube 717 | picket fence, paling 718 | pickup, pickup truck 719 | pier 720 | piggy bank, penny bank 721 | pill bottle 722 | pillow 723 | ping-pong ball 724 | pinwheel 725 | pirate, pirate ship 726 | pitcher, ewer 727 | plane, carpenter's plane, woodworking plane 728 | planetarium 729 | plastic bag 730 | plate rack 731 | plow, plough 732 | plunger, plumber's helper 733 | Polaroid camera, Polaroid Land camera 734 | pole 735 | police van, police wagon, paddy wagon, patrol wagon, wagon, black Maria 736 | poncho 737 | pool table, billiard table, snooker table 738 | pop bottle, soda bottle 739 | pot, flowerpot 740 | potter's wheel 741 | power drill 742 | prayer rug, prayer mat 743 | printer 744 | prison, prison house 745 | projectile, missile 746 | projector 747 | puck, hockey puck 748 | punching bag, punch bag, punching ball, punchball 749 | purse 750 | quill, quill pen 751 | quilt, comforter, comfort, puff 752 | racer, race car, racing car 753 | racket, racquet 754 | radiator 755 | radio, wireless 756 | radio telescope, radio reflector 757 | rain barrel 758 | recreational vehicle, RV, R.V. 759 | reel 760 | reflex camera 761 | refrigerator, icebox 762 | remote control, remote 763 | restaurant, eating house, eating place, eatery 764 | revolver, six-gun, six-shooter 765 | rifle 766 | rocking chair, rocker 767 | rotisserie 768 | rubber eraser, rubber, pencil eraser 769 | rugby ball 770 | rule, ruler 771 | running shoe 772 | safe 773 | safety pin 774 | saltshaker, salt shaker 775 | sandal 776 | sarong 777 | sax, saxophone 778 | scabbard 779 | scale, weighing machine 780 | school bus 781 | schooner 782 | scoreboard 783 | screen, CRT screen 784 | screw 785 | screwdriver 786 | seat belt, seatbelt 787 | sewing machine 788 | shield, buckler 789 | shoe shop, shoe-shop, shoe store 790 | shoji 791 | shopping basket 792 | shopping cart 793 | shovel 794 | shower cap 795 | shower curtain 796 | ski 797 | ski mask 798 | sleeping bag 799 | slide rule, slipstick 800 | sliding door 801 | slot, one-armed bandit 802 | snorkel 803 | snowmobile 804 | snowplow, snowplough 805 | soap dispenser 806 | soccer ball 807 | sock 808 | solar dish, solar collector, solar furnace 809 | sombrero 810 | soup bowl 811 | space bar 812 | space heater 813 | space shuttle 814 | spatula 815 | speedboat 816 | spider web, spider's web 817 | spindle 818 | sports car, sport car 819 | spotlight, spot 820 | stage 821 | steam locomotive 822 | steel arch bridge 823 | steel drum 824 | stethoscope 825 | stole 826 | stone wall 827 | stopwatch, stop watch 828 | stove 829 | strainer 830 | streetcar, tram, tramcar, trolley, trolley car 831 | stretcher 832 | studio couch, day bed 833 | stupa, tope 834 | submarine, pigboat, sub, U-boat 835 | suit, suit of clothes 836 | sundial 837 | sunglass 838 | sunglasses, dark glasses, shades 839 | sunscreen, sunblock, sun blocker 840 | suspension bridge 841 | swab, swob, mop 842 | sweatshirt 843 | swimming trunks, bathing trunks 844 | swing 845 | switch, electric switch, electrical switch 846 | syringe 847 | table lamp 848 | tank, army tank, armored combat vehicle, armoured combat vehicle 849 | tape player 850 | teapot 851 | teddy, teddy bear 852 | television, television system 853 | tennis ball 854 | thatch, thatched roof 855 | theater curtain, theatre curtain 856 | thimble 857 | thresher, thrasher, threshing machine 858 | throne 859 | tile roof 860 | toaster 861 | tobacco shop, tobacconist shop, tobacconist 862 | toilet seat 863 | torch 864 | totem pole 865 | tow truck, tow car, wrecker 866 | toyshop 867 | tractor 868 | trailer truck, tractor trailer, trucking rig, rig, articulated lorry, semi 869 | tray 870 | trench coat 871 | tricycle, trike, velocipede 872 | trimaran 873 | tripod 874 | triumphal arch 875 | trolleybus, trolley coach, trackless trolley 876 | trombone 877 | tub, vat 878 | turnstile 879 | typewriter keyboard 880 | umbrella 881 | unicycle, monocycle 882 | upright, upright piano 883 | vacuum, vacuum cleaner 884 | vase 885 | vault 886 | velvet 887 | vending machine 888 | vestment 889 | viaduct 890 | violin, fiddle 891 | volleyball 892 | waffle iron 893 | wall clock 894 | wallet, billfold, notecase, pocketbook 895 | wardrobe, closet, press 896 | warplane, military plane 897 | washbasin, handbasin, washbowl, lavabo, wash-hand basin 898 | washer, automatic washer, washing machine 899 | water bottle 900 | water jug 901 | water tower 902 | whiskey jug 903 | whistle 904 | wig 905 | window screen 906 | window shade 907 | Windsor tie 908 | wine bottle 909 | wing 910 | wok 911 | wooden spoon 912 | wool, woolen, woollen 913 | worm fence, snake fence, snake-rail fence, Virginia fence 914 | wreck 915 | yawl 916 | yurt 917 | web site, website, internet site, site 918 | comic book 919 | crossword puzzle, crossword 920 | street sign 921 | traffic light, traffic signal, stoplight 922 | book jacket, dust cover, dust jacket, dust wrapper 923 | menu 924 | plate 925 | guacamole 926 | consomme 927 | hot pot, hotpot 928 | trifle 929 | ice cream, icecream 930 | ice lolly, lolly, lollipop, popsicle 931 | French loaf 932 | bagel, beigel 933 | pretzel 934 | cheeseburger 935 | hotdog, hot dog, red hot 936 | mashed potato 937 | head cabbage 938 | broccoli 939 | cauliflower 940 | zucchini, courgette 941 | spaghetti squash 942 | acorn squash 943 | butternut squash 944 | cucumber, cuke 945 | artichoke, globe artichoke 946 | bell pepper 947 | cardoon 948 | mushroom 949 | Granny Smith 950 | strawberry 951 | orange 952 | lemon 953 | fig 954 | pineapple, ananas 955 | banana 956 | jackfruit, jak, jack 957 | custard apple 958 | pomegranate 959 | hay 960 | carbonara 961 | chocolate sauce, chocolate syrup 962 | dough 963 | meat loaf, meatloaf 964 | pizza, pizza pie 965 | potpie 966 | burrito 967 | red wine 968 | espresso 969 | cup 970 | eggnog 971 | alp 972 | bubble 973 | cliff, drop, drop-off 974 | coral reef 975 | geyser 976 | lakeside, lakeshore 977 | promontory, headland, head, foreland 978 | sandbar, sand bar 979 | seashore, coast, seacoast, sea-coast 980 | valley, vale 981 | volcano 982 | ballplayer, baseball player 983 | groom, bridegroom 984 | scuba diver 985 | rapeseed 986 | daisy 987 | yellow lady's slipper, yellow lady-slipper, Cypripedium calceolus, Cypripedium parviflorum 988 | corn 989 | acorn 990 | hip, rose hip, rosehip 991 | buckeye, horse chestnut, conker 992 | coral fungus 993 | agaric 994 | gyromitra 995 | stinkhorn, carrion fungus 996 | earthstar 997 | hen-of-the-woods, hen of the woods, Polyporus frondosus, Grifola frondosa 998 | bolete 999 | ear, spike, capitulum 1000 | toilet tissue, toilet paper, bathroom tissue'''.split("\n") --------------------------------------------------------------------------------