def feature_extraction(imgs): path = "./face_feature.png" batch = [] if len(imgs) > 0: if imgs.ndim > 3: for i in range(imgs.shape[0]): cv2.imwrite("./face_feature.png", imgs[i]) # 调用开源代码实现的函数load_image加载人脸图像 img = utils.load_image(path) batch.append(img) else: cv2.imwrite("./face_feature.png", imgs) img = utils.load_image(path) batch.append(img.reshape((1, 224, 224, 3))) batch = np.concatenate(batch) with tf.Session() as sess: feed_dict = {input_: batch} # 前向传播获得vgg.fc7(就是第二个全连接层)的输出作为人脸特征 feature = sess.run(vgg.fc7, feed_dict=feed_dict) if imgs.ndim == 3: feature = np.reshape(feature, (1, 4096)) # 返回人脸特征 return feature
def predict(self, vgg, test_img_path, lb): # Run this cell if you don't have a vgg graph built test_img = imread(test_img_path) with tf.Session() as sess: input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) vgg.build(input_) with tf.Session() as sess: img = utils.load_image(test_img_path) img = img.reshape((1, 224, 224, 3)) feed_dict = {input_: img} code = sess.run(vgg.relu6, feed_dict=feed_dict) with tf.Session() as sess: self.saver.restore(sess, "checkpoints/%s.ckpt" % self.checkpoint) feed = {self.inputs_: code} prediction = sess.run(self.predicted, feed_dict=feed).squeeze() plt.imshow(test_img) plt.show() plt.barh(np.arange(5), prediction) _ = plt.yticks(np.arange(5), lb.classes_) plt.show()
def predict_img_result(img_src, input_, sess, vgg, graph): print('img_src: ' + str(img_src)) img = utils.load_image(img_src) print('' + str(img.shape)) img = img.reshape((1, 224, 224, 3)) #################### #tf.reset_default_graph() # Now, let's access and create placeholders variables and # create feed-dict to feed new data feed_dict = {input_: img} code = sess.run(vgg.relu6, feed_dict=feed_dict) inputs_ = graph.get_tensor_by_name('inputs:0') predicted = graph.get_tensor_by_name('op_predicted:0') feed = {inputs_: code} # 预测结果分别表示为['bird' 'cat' 'dog' 'fish' 'tiger'] # prediction是一个list prediction = sess.run(predicted, feed_dict=feed).squeeze() print(prediction) #sess.close() return prediction.tolist()
def classifier(image_path): g1 = tf.Graph() with g1.as_default(): test_input = tf.placeholder(tf.float32, shape=[None, 4096]) # 加入一个256维的全连接的层 fc = tf.contrib.layers.fully_connected(test_input, 256) # 加入一个5维的全连接层 logits = tf.contrib.layers.fully_connected(fc, 5, activation_fn=None) predicted = tf.nn.softmax(logits) saver = tf.train.Saver() with tf.Session() as sess: vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): # 载入VGG16模型 vgg.build(input_) ckpt = tf.train.get_checkpoint_state('checkpoints') saver.restore(sess, ckpt.model_checkpoint_path) img = utils.load_image(image_path) img = img.reshape((1, 224, 224, 3)) test_batch = [] test_batch.append(img) image_one = np.concatenate(test_batch) feed1 = {input_: image_one} img_feature = sess.run(vgg.relu6, feed_dict=feed1) feed = {test_input: img_feature} res = sess.run(predicted, feed_dict=feed) test_acc = sess.run(predicted, feed_dict=feed) return np.argmax(test_acc)
def detect_hash(self, path, sess): self.img = utils.load_image(path) self.img = self.img.reshape((1, 224, 224, 3)) # print("Getting Hash for "+path) self.feed_dict = {self.input_: self.img} self.code = sess.run(self.vgg.relu6, feed_dict=self.feed_dict) return (self.code[0])
def main(): parser = argparse.ArgumentParser() parser.add_argument('input_image', type=str, default='/path/to/image', help='path to image.') parser.add_argument('vgg16_path', type=str, default='/path/to/vgg16.npy', help='path to vgg16.npy.') parser.add_argument('--top_n', type=int, default=3, help="Grad-CAM for top N predicted classes.") args = parser.parse_args() print(args) input_image = utils.load_image(args.input_image) # tf RGB image_batch = input_image[None, :, :, :3] graph = tf.Graph() sess = tf.InteractiveSession(graph=graph) with tf.device('/cpu:0'): images = tf.placeholder("float", [None, 224, 224, 3]) model = vgg16.Vgg16(vgg16_npy_path=args.vgg16_path) with tf.name_scope("content_vgg"): model.build(images) path_synset = os.path.join(os.path.dirname(vgg16.__file__), "synset.txt") prob = sess.run(model.prob, feed_dict={images: image_batch}) infos = get_info(prob[0], path_synset, top_n=args.top_n) for rank, info in enumerate(infos): print("{}: class id: {}, class name: {}, probability: {:.3f}, synset: {}".format(rank, *info)) # GRAD-CAM for i in range(args.top_n): class_id = infos[i][0] class_name = infos[i][1] prob = infos[i][2] cams = grad_cam(model, class_id, "content_vgg/conv5_3/Relu", sess, feed_dict={images: image_batch}) save_cam(cams, i, class_id, class_name, prob, image_batch, args.input_image) # Guided Backpropagation register_gradient() del model images = tf.placeholder("float", [None, 224, 224, 3]) guided_model = everride_relu('GuidedBackPropReLU', images, args.vgg16_path) class_id = infos[0][0] class_saliencies = saliency_by_class(guided_model, images, class_id, nb_classes=1000) class_saliency = sess.run(class_saliencies, feed_dict={images: image_batch})[0][0] class_saliency = class_saliency - class_saliency.min() class_saliency = class_saliency / class_saliency.max() * 255.0 base_path, ext = os.path.splitext(args.input_image) gbprop_path = "{}_{}{}".format(base_path, "guided_bprop", ext) cv2.imwrite(gbprop_path, class_saliency.astype(np.uint8))
def extract_training_features(): data_dir = 'data_gen/' contents = os.listdir(data_dir) classes = [each for each in contents if os.path.isdir(data_dir + each)] batch_size = 10 codes_list = [] labels = [] batch = [] codes = None with tf.Session() as sess: # Construct VGG16 object vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): # build VGG16 model vgg.build(input_) # for every kind of flower, use VGG16 to calculate its feature for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # 载入图片并放入batch数组中 img = utils.load_image(os.path.join(class_path, file)) batch.append(img.reshape((1, 224, 224, 3))) labels.append(each) # 如果图片数量到了batch_size则开始具体的运算 if ii % batch_size == 0 or ii == len(files): images = np.concatenate(batch) feed_dict = {input_: images} # 计算特征值 codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) # 将结果放入到codes数组中 if codes is None: codes = codes_batch else: codes = np.concatenate((codes, codes_batch)) # 清空数组准备下一个batch的计算 batch = [] print('{} images processed'.format(ii)) np.save("train_features.npy", codes) np.save("train_labels.npy", np.array(labels))
def run(self): diter = self.parent offset = 0 i = 0 while not diter.exit_flag: last_batch = False image_batch = np.zeros((diter.batch_size,224,224,3)) caption_batch_x = np.zeros((diter.batch_size,diter.max_caption_length)) caption_batch_y = np.zeros((diter.batch_size,diter.max_caption_length)) mask = np.zeros((diter.batch_size,diter.max_caption_length)) counter = 0 while counter < diter.batch_size: if offset == diter.num_data_points: if not diter.use_infinite_loop: print("Hello") last_batch = True diter.queue.put(None) return else: offset = 0 print("End") index = self.indices[offset] (caption_id,image_file) = diter.caption_to_image_dict[index] caption = diter.processed_captions[caption_id] image = utils.load_image(os.path.join(diter.image_path,image_file)) caption_x,caption_y = process(caption,diter.max_caption_length) mask[counter,:len(caption_x)] = 1 caption_batch_x[counter,:len(caption_x)] = caption_x caption_batch_y[counter,:len(caption_y)] = caption_y image_batch[counter,:,:,:] = image.reshape((224, 224, 3)) counter += 1 offset += 1 if counter == diter.batch_size: batch = {} batch['image_batch'] = image_batch batch['caption_batch_x'] = caption_batch_x batch['caption_batch_y'] = caption_batch_y batch['mask'] = mask diter.queue.put(batch) i+=1 if last_batch: diter.queue.put(None) return
def generate_features(classes, data_dir, vgg, batch_size=10): codes_list = [] labels = [] batch = [] codes = None with tf.Session() as sess: # vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): vgg.build(input_) for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # Add images to the current batch # utils.load_image crops the input images for us, from the center try: img = utils.load_image(os.path.join(class_path, file)) batch.append(img.reshape((1, 224, 224, 3))) labels.append(each) # Running the batch through the network to get the codes if ii % batch_size == 0 or ii == len(files): images = np.concatenate(batch) feed_dict = {input_: images} codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) # Here I'm building an array of the codes if codes is None: codes = codes_batch else: codes = np.concatenate((codes, codes_batch)) # Reset to start building the next batch batch = [] print('{} images processed'.format(ii)) except Exception as e: print(e) return codes, labels
def create_data(): data_dir = r'flower_photos/' contents = os.listdir(data_dir) classes = [each for each in contents if os.path.isdir(data_dir + each)] batch_size = 10 codes_list = [] # 存放特征值 labels = [] # 存放花的类别 batch = [] # 存放图片数据 codes = None with tf.Session() as sess: vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32,[None,224,224,3]) with tf.name_scope('content_vgg'): vgg.build(input_) #为每个不同类别的花分别用vgg16计算特征值 for each in classes: print("starting {} images".format(each)) class_path = data_dir+each files = os.listdir(class_path) #枚举每种花的文件夹中图片 for ii,file in enumerate(files,1): img = utils.load_image(os.path.join(class_path,file)) batch.append(np.reshape(img,(1,224,224,3))) labels.append(each) if ii%batch_size==0 or ii==len(files): images = np.concatenate(batch) #s数组拼接 feed_dict = {input_:images} #计算特征值 codes_batch = sess.run(vgg.relu6,feed_dict=feed_dict) #得到第一层全连接层之后的特征值 #将结果加入到codes数组中 if codes is None: codes = codes_batch else: codes = np.concatenate((codes,codes_batch)) batch=[] print('{} images processed'.format(ii)) return codes,labels
def extract_testing_features(): batch_size = 10 batch = [] codes = None df_test = pd.read_csv('./sample_submission.csv') with tf.Session() as sess: # Construct VGG16 object vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): # build VGG16 model vgg.build(input_) files = df_test['id'].values for ii, file in enumerate(files, 1): # 载入图片并放入batch数组中 img = utils.load_image('./test/{}.jpg'.format(file)) batch.append(img.reshape((1, 224, 224, 3))) # 如果图片数量到了batch_size则开始具体的运算 if ii % batch_size == 0 or ii == len(files): images = np.concatenate(batch) feed_dict = {input_: images} # 计算特征值 codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) # 将结果放入到codes数组中 if codes is None: codes = codes_batch else: codes = np.concatenate((codes, codes_batch)) # 清空数组准备下一个batch的计算 batch = [] print('{} images processed'.format(ii)) np.save("test_features.npy", codes)
vgg.build(input_) loader = tf.train.import_meta_graph('checkpoints/mugs.ckpt.meta') with tf.Session() as sess: loader.restore(sess, tf.train.latest_checkpoint('checkpoints')) graph = tf.get_default_graph() inputs_ = graph.get_tensor_by_name("inputs:0") predicted = graph.get_tensor_by_name("predicted:0") count = 0 while (count < 10): test_img, test_img_path, true_class = get_random_img() img = utils.load_image(test_img_path) img = img.reshape((1, 224, 224, 3)) feed_dict = {input_: img} code = sess.run(vgg.relu6, feed_dict=feed_dict) feed = {inputs_: code} prediction = sess.run(predicted, feed_dict=feed).squeeze() print(true_class) print(predicted) # Plot image and class predictions plt.figure() plt.subplot(211) plt.imshow(test_img)
codes = None with tf.Session() as sess: vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): vgg.build(input_) for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # Add images to the current batch # utils.load_image crops the input images for us, from the center img = utils.load_image(os.path.join(class_path, file)) batch.append(img.reshape((1, 224, 224, 3))) labels.append(each) # Running the batch through the network to get the codes if ii % batch_size == 0 or ii == len(files): images = np.concatenate(batch) feed_dict = {input_: images} codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) # Here I'm building an array of the codes if codes is None: codes = codes_batch else: codes = np.concatenate((codes, codes_batch))
import numpy as np import tensorflow as tf from tensorflow_vgg import vgg16 from tensorflow_vgg import utils img1 = utils.load_image("./test_data/tiger.jpeg") img2 = utils.load_image("./test_data/puzzle.jpeg") batch1 = img1.reshape((1, 224, 224, 3)) batch2 = img2.reshape((1, 224, 224, 3)) batch = np.concatenate((batch1, batch2), 0) # with tf.Session(config=tf.ConfigProto(gpu_options=(tf.GPUOptions(per_process_gpu_memory_fraction=0.7)))) as sess: with tf.device('/cpu:0'): with tf.Session() as sess: images = tf.placeholder("float", [2, 224, 224, 3]) feed_dict = {images: batch} vgg = vgg16.Vgg16() with tf.name_scope("content_vgg"): vgg.build(images) prob = sess.run(vgg.prob, feed_dict=feed_dict) print(prob) utils.print_prob(prob[0], './synset.txt') utils.print_prob(prob[1], './synset.txt')
# predicted class predicted = tf.nn.softmax(logits) # accuracy correct_pred = tf.equal(tf.argmax(predicted, 1), tf.argmax(labels_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32)) # save model saver = tf.train.Saver() with tf.Session() as sess: # build VGG 16 vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [1, 224, 224, 3]) with tf.name_scope('content_vgg'): # load model vgg.build(input_) img = utils.load_image('./test_vgg/9/001.png') img1=img.reshape((1, 224, 224, 3)) feed_dict = {input_: img1} codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) # codes_batch [n,4096] codes = codes_batch saver.restore(sess, tf.train.latest_checkpoint('checkpoints')) feed = {inputs_: codes} prob = sess.run(predicted, feed_dict=feed)[0] preds = (np.argsort(prob)[::-1])[0:5] for p in preds: print(classes[p], prob[p])
with tf.Session() as sess: # Build the vgg network vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope('content_vgg'): vgg.build(input_) for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # Add images to the current batch # utils.load_image crops the input images from the center to resize it to the required input size for VGG img = utils.load_image(os.path.join(class_path, file)) # outputs ndarray # os.path.join(class_path, file) same as class_path+'|'+file batch.append(img.reshape((1, 224, 224, 3))) # format: [array([...]), array([...])] with each array being an image labels.append(each) # Running the batch through the network to get the codes if ii % batch_size == 0 or ii == len(files): # whenever we have enough pictures in our batch or we've reached the end of the folder # Image batch to pass to VGG network images = np.concatenate(batch) # format: [[...] [...]] with each [...] being an image # Get the values from the relu6 layer of the VGG network codes_batch = sess.run(vgg.relu6, feed_dict={input_: images}) # Build an array of codes concatenated across batches if codes is None: codes = codes_batch
flags = tf.flags logging = tf.logging flags.DEFINE_string("log_path", None, "Log Directory path") FLAGS = flags.FLAGS annFile = '/data/lisatmp4/chitwan/mscoco/annotations/captions_val2014.json' coco = COCO(annFile) imgid_list = coco.getImgIds() image = coco.loadImgs(imgid_list[150]) print(image[0]['file_name']) image = utils.load_image( os.path.join('/data/lisatmp4/chitwan/mscoco/val2014_modified/', image[0]['file_name'])) vocabulary = cPickle.load( open('/data/lisatmp4/chitwan/mscoco/caption_processed/vocabulary.pkl', 'r')) def main(_): model_config = imp.load_source('config', 'config/config.py').config().image log_path = os.path.join(FLAGS.log_path, 'hybrid_log') with tf.Graph().as_default(): main_model = image_captioning_sampler.ImageCaptioning(model_config)
with tf.Session() as sess: with tf.Session() as sess: vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope("content_vgg"): vgg.build(input_) for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # Add images to the current batch img = utils.load_image(os.path.join(class_path, file)) batch.append(img.reshape((1, 224, 224, 3))) labels.append(each) # Running the batch through the network to get the codes if ii % batch_size == 0 or ii == len(files): # Image batch to pass to VGG network, from high dimension data batch, convert it into 1d images = np.concatenate(batch) # Get the values from the relu6 layer of the VGG network feed_dict = {input_: images} codes_batch = sess.run(vgg.relu6, feed_dict=feed_dict) print(codes_batch.shape) # store the codes in an array
# Build the vgg network vgg = vgg16.Vgg16() input_ = tf.placeholder(tf.float32, [None, 224, 224, 3]) with tf.name_scope('content_vgg'): vgg.build(input_) for each in classes: print("Starting {} images".format(each)) class_path = data_dir + each files = os.listdir(class_path) for ii, file in enumerate(files, 1): # Add images to the current batch # utils.load_image crops the input images from the center to resize it to the required input size for VGG img = utils.load_image( os.path.join(class_path, file) ) # outputs ndarray # os.path.join(class_path, file) same as class_path+'|'+file batch.append( img.reshape((1, 224, 224, 3)) ) # format: [array([...]), array([...])] with each array being an image labels.append(each) # Running the batch through the network to get the codes if ii % batch_size == 0 or ii == len( files ): # whenever we have enough pictures in our batch or we've reached the end of the folder # Image batch to pass to VGG network images = np.concatenate( batch ) # format: [[...] [...]] with each [...] being an image