def classify_from_url(url): image_string = urllib.urlopen(url).read() image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_resnet_v2_arg_scope()): logits, _ = inception.inception_resnet_v2(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, checkpoints_filename), slim.get_model_variables(model_name)) init_fn(sess) np_image, probabilities = sess.run([image, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])] plt.figure() plt.imshow(np_image.astype(np.uint8)) plt.axis('off') plt.show() names = imagenet.create_readable_names_for_imagenet_labels() for i in range(5): index = sorted_inds[i] print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index]))
def readImage(current_jpeg_image): img_size = resized_dimention[FLAGS.model_name] image_data = tf.read_file(current_jpeg_image) image_data = tf.image.decode_jpeg(image_data, channels=3) # (InceptionResnetV2 does not need prepreocessing) if models[FLAGS.model_name] != Models.inceptionresnetv2.value: if models[FLAGS.model_name] < Models.inceptionresnetv2.value: image_data = inception_preprocessing.preprocess_image( image_data, img_size, img_size, is_training=False) else: # Different preprocessing for vgg19 and vgg16 (this reading is faulty now) image_data = vgg_preprocessing.preprocess_image(image_data, img_size, img_size, is_training=False) # image_data = inception_preprocessing.preprocess_image(image_data, img_size, img_size, is_training=False) image_data = tf.expand_dims(image_data, 0) # print(image_data) # exit() return image_data
def classify_IR(self, sample_images): try: # Prerape the image for evalution start_time = time.time() image_rawIR = tf.image.decode_image(tf.read_file(sample_images), channels=3) imageIR = inception_preprocessing.preprocess_image( image_rawIR, self.image_sizeIR, self.image_sizeIR, is_training=False) imageIR = tf.expand_dims(imageIR, 0) imageIR = self.sessIR.run(imageIR) # Run NN predsIR = self.sessIR.run( self.probabilitiesIR, feed_dict={self.processed_imagesIR: imageIR}) end_time = time.time() # Prepare results for web-page probsIR = predsIR[0, 0:] sorted_indsIR = [ i[0] for i in sorted(enumerate(-probsIR), key=lambda x: x[1]) ] class_name = [] probabilities_for_class = [] for i in range(3): indexIR = sorted_indsIR[i] class_name.append(self.labels_names[indexIR]) probabilities_for_class.append(probsIR[indexIR]) return [class_name, probabilities_for_class, end_time - start_time] except Exception as err: return None
def main(opt): config = tf.ConfigProto(allow_soft_placement=True) gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1) config.gpu_options.allow_growth = True jpg_path = opt.image_path images_lists = [] for subdir, dirs, files in os.walk(jpg_path): for f in files: f = f.strip() images_lists.append(os.path.join(jpg_path, f)) att_dir = os.path.join(opt.out_dir, opt.att_dir) fc_dir = os.path.join(opt.out_dir, opt.fc_dir) if not tf.gfile.Exists(fc_dir): tf.gfile.MakeDirs(fc_dir) if not tf.gfile.Exists(att_dir): tf.gfile.MakeDirs(att_dir) checkpoints_dir = opt.model_path slim = tf.contrib.slim image_size = inception.inception_v3.default_image_size tf_image = tf.placeholder(tf.string, None) image = tf.image.decode_jpeg(tf_image, channels=3) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) with slim.arg_scope(inception.inception_v3_arg_scope()): tf_feats_att, tf_feats_fc = inception.inception_v3(processed_images, num_classes=1001, is_training=False) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v3.ckpt'), slim.get_model_variables('InceptionV3')) with tf.Session(config=config) as sess: init_fn(sess) for idx, image_path in enumerate(images_lists): print('{} {}'.format(idx, image_path)) image_name = os.path.basename(image_path) image_id = get_image_id(image_name) url = 'file://' + image_path image_string = urllib.request.urlopen(url).read() conv_feats, fc_feats = sess.run([tf_feats_att, tf_feats_fc], feed_dict={tf_image: image_string}) conv_feats = np.squeeze(conv_feats) fc_feats = np.squeeze(fc_feats) np.save(os.path.join(fc_dir, str(image_id)), fc_feats) np.savez_compressed(os.path.join(att_dir, str(image_id)), feat=conv_feats)
def get_data(): images = [] processed_images_batch = [] for line in open("/Users/fuchao/pictest.txt"): url = line.split(',')[0].strip() print url request = urllib2.Request(url) request.add_header( 'User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' ) try: response = urllib2.urlopen(request) except urllib2.URLError, e: print e.code continue # Open specified url and load image as a string image_string = urllib2.urlopen(url).read() image = tf.image.decode_image(image_string, channels=3) # Resize the input image, preserving the aspect ratio # and make a central crop of the resulted image. # The crop will be of the size of the default image size of # the network. #print image processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) # Networks accept images in batches. # The first dimension usually represents the batch size. # In our case the batch size is one. processed_images = tf.expand_dims(processed_image, 0) images.append(processed_images) processed_images_batch.append(processed_image) lables_map[line.split(',')[0].strip()] = line.split(',')[1].strip()
def get_image_data(sess, imgID, QF, isCast = True): # Parse the YUV and convert it into RGB original_img_ID = imgID imgID = str(imgID).zfill(8) shard_num = math.floor((original_img_ID - 1) / 10000) folder_num = math.ceil(original_img_ID/1000); if isCast: path_to_recons = PATH_TO_RECONS # Get files list to fetch the correct name filesList = glob.glob(path_to_recons + str(folder_num) + '/ILSVRC2012_val_' + imgID + '*.yuv') name = filesList[0].split('/')[-1] rgbStr = name.split('_')[5] width = int(name.split('_')[-5]) height = int(name.split('_')[-4]) is_gray_str = name.split('_')[-3] image = path_to_recons + str(folder_num) + '/ILSVRC2012_val_' + imgID + '_' + str(width) + '_' + str(height) + '_' + rgbStr + '_' + str(QF) + '_1.yuv' size = (height, width) # height and then width videoObj = VideoCaptureYUV(image, size, isGrayScale=is_gray_str.__contains__('Y')) ret, yuv, rgb = videoObj.getYUVAndRGB() image_data = rgb image_data = tf.convert_to_tensor(image_data, dtype=tf.uint8) else: if QF == 110: image = path_to_valid_images + str(folder_num) + '/ILSVRC2012_val_' + imgID + '.JPEG' else: image = path_to_valid_QF_images + str(folder_num) + '/ILSVRC2012_val_' + imgID + '-QF-' + str(QF) + '.JPEG' image_data = tf.read_file(image) image_data = tf.image.decode_jpeg(image_data, channels=3) image_data = inception_preprocessing.preprocess_image(image_data, 299, 299, is_training=False) image_data = tf.expand_dims(image_data, 0) return image_data
def transform_data(img, is_training): image_dense = tf.map_fn(lambda x: inception_preprocessing.preprocess_image( x, image_size, image_size, is_training=is_training, crop=is_training, add_image_summaries=False, fast_mode=False), img, dtype=tf.float32) image_dense = tf.to_float( tf.image.convert_image_dtype(image_dense, tf.uint8)) image_aux = image_dense image_dense = densenet_preprocessing._SCALE_FACTOR * tf.map_fn( lambda x: densenet_preprocessing._mean_image_subtraction( x, [ densenet_preprocessing._R_MEAN, densenet_preprocessing._G_MEAN, densenet_preprocessing._B_MEAN ]), image_dense, dtype=tf.float32) return image_dense
def get_Image_Batch_From_Tfrecord_2(dataset, batch_size=FLAGS.batch_size, shuffle=True, enqueue_many=False): data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=32, common_queue_min=8) image_raw, label = data_provider.get(['image', 'label']) # Preprocess image for usage by Inception. image = inception_preprocessing.preprocess_image(image_raw, 299, 299, is_training=False) if shuffle: imageBatch, labelBatch = tf.train.shuffle_batch( [image, label], batch_size=batch_size, capacity=2 * batch_size, min_after_dequeue=1, enqueue_many=enqueue_many) else: imageBatch, labelBatch = tf.train.batch([image, label], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) return imageBatch, labelBatch
def _parse_function_train(filename, label): # # load and preprocess the image img_string = tf.read_file(filename) image = tf.image.decode_jpeg(img_string, channels=3) image = tf.to_float(image) / 255.0 image = preprocess_image(image, FLAGS.image_size, FLAGS.image_size, is_training=True, fast_mode=True) return image, label
def load_ckpt(to_save_pb=False): tf.reset_default_graph() checkpoint_file = 'big_ckpt/inception_resnet_v2_2016_08_30.ckpt' image = tf.image.decode_jpeg(tf.read_file('pics/dog.jpg'), channels=3) # ['dog.jpg', 'panda.jpg'] processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False,) # 这个函数做了裁剪,缩放和归一化等 processed_images = tf.expand_dims(processed_image, 0) arg_scope = inception_resnet_v2_arg_scope() # Creates the Inception Resnet V2 model. with slim.arg_scope(arg_scope): logits, end_points = inception_resnet_v2(processed_images, is_training=False) probabilities = tf.nn.softmax(logits, name='final') saver = tf.train.Saver() if to_save_pb: convert_ckpt_to_pb(saver) return exit(0) with tf.Session() as sess: saver.restore(sess, checkpoint_file) start = time() # predict_values, logit_values = sess.run([end_points['Predictions'], logits]) image2, network_inputs, probabilities2 = sess.run([image, processed_images, probabilities]) end = time() print('ckpt time cost', end - start) # 2.4201557636260986
def get_embeddings(instances): image_size = 299 query_embeddings = {} with tf.Graph().as_default(): image = tf.placeholder(tf.uint8, (None, None, 3)) processed_image = inception_preprocessing.preprocess_image( image, image_size, image_size, is_training=False) processed_image = tf.expand_dims(processed_image, 0) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_101(processed_image, 1001, is_training=False) pool5 = tf.get_default_graph().get_tensor_by_name( "resnet_v2_101/pool5:0") init_fn = slim.assign_from_checkpoint_fn( 'resnet_v2_101.ckpt', slim.get_model_variables('resnet_v2')) with tf.Session() as sess: init_fn(sess) for cls_id in instances.keys(): ins, patch = instances[cls_id] scaled_img, logit_vals, embedding = sess.run( [processed_image, logits, pool5], feed_dict={image: patch}) query_embeddings[cls_id] = embedding[0, 0, 0, :] return query_embeddings
def load_batch(dataset, batch_size, height=256, width=256, is_training=False): data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, num_readers=64, common_queue_capacity=20 * batch_size, common_queue_min=10 * batch_size) image, label = data_provider.get(['image', 'label']) image = inception_preprocessing.preprocess_image( image, height, width, is_training) images, labels = tf.train.batch( [image, label], batch_size=batch_size, # allow_smaller_final_batch=True, num_threads=16, capacity=10 * batch_size, ) batch_queue = slim.prefetch_queue.prefetch_queue( [images, labels], num_threads=32, capacity=5 * batch_size) return batch_queue
def extract_color(self, image_data): try: MODEL = os.environ['CLASSIFY_GRAPH'] LABEL = os.environ['LABEL_MAP'] GPU_NUM = os.environ['GPU_NUM'] GPU = '/device:GPU:' + GPU_NUM #MODEL = '/Users/lion/PycharmProjects/stylelens-color/color_classification_model.pb' #LABEL = '/Users/lion/PycharmProjects/bl-color/labels.txt' except: print( "!!! Need to define environment variable: CLASSIFY_GRAPH=/path/to/model.pb" ) with tf.gfile.FastGFile(MODEL, 'rb') as f: print(MODEL) graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(graph_def, name='') image_data = tf.image.decode_jpeg(image_data, channels=3) image_data = inception_preprocessing.preprocess_image( image_data, 299, 299, is_training=False) image_data = tf.expand_dims(image_data, 0) #LABEL = '/Users/lion/PycharmProjects/bl-color/labels.txt' print(LABEL) #label 받아오기 label_dic = [ 'black', 'blue', 'brown', 'gray', 'green', 'mint', 'orange', 'pink', 'purple', 'red', 'white', 'yellow' ] """ label_f = open(LABEL,'rb') lines = label_f.readlines() for w in lines: w = str(w).replace('b\'', "") w = w.replace("\\n", "") labels_data = w.split(':') label_dic.append(labels_data[1]) """ #with tf.device(GPU): with tf.Session() as self.sess: self.softmax_tensor = self.sess.graph.get_tensor_by_name( 'InceptionV3/Predictions/Softmax:0') img = image_data.eval() img = img.tolist() self.sess.graph predict_color = self.sess.run(self.softmax_tensor, {'input:0': img}) predict_color = np.squeeze(predict_color) color = predict_color.argmax() answer_label = label_dic[color] answer_score = predict_color[color] return answer_label, answer_score
def transform_img_fn(path_list): out = [] for f in path_list: image_raw = tf.image.decode_jpeg(open(f, 'rb').read(), channels=3) image = inception_preprocessing.preprocess_image(image_raw, image_size, image_size, is_training=False) out.append(image) return session.run([out])[0]
def __init__(self): self.slim = tf.contrib.slim self.image_size = inception_v4.inception_v4.default_image_size self.checkpoints_dir = 'checkpoints' self.names = imagenet.create_readable_names_for_imagenet_labels() self.arg_scope = inception_v4.inception_v4_arg_scope() self.image = tf.placeholder(tf.uint8, [480, 640, 3]) self.processed_image = inception_preprocessing.preprocess_image(self.image, self.image_size, self.image_size, is_training=False) self.processed_images = tf.expand_dims(self.processed_image, 0) # processed_images will be a 1x299x299x3 tensor of float32 # Create the model, use the default arg scope to configure the batch norm parameters. with self.slim.arg_scope(self.arg_scope): self.logits, self.end_points = inception_v4.inception_v4(self.processed_images, num_classes=1001, is_training=False) self.probs = tf.nn.softmax(self.logits) self.init_fn = self.slim.assign_from_checkpoint_fn( os.path.join(self.checkpoints_dir, 'inception_v4.ckpt'), self.slim.get_model_variables('InceptionV4')) config = tf.compat.v1.ConfigProto() config.gpu_options.allow_growth = True self.session = tf.compat.v1.Session(config=config) self.init_fn(self.session)
def load_batch(dataset, batch_size=32, height=299, width=299, is_training=False): data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, common_queue_capacity=32, common_queue_min=8) image_raw, label = data_provider.get(['image', 'label']) # Preprocess image for usage by Inception. image = inception_preprocessing.preprocess_image(image_raw, height, width, is_training=is_training) # Preprocess the image for display purposes. image_raw = tf.expand_dims(image_raw, 0) image_raw = tf.image.resize_images(image_raw, [height, width]) image_raw = tf.squeeze(image_raw) # Batch it up. images, images_raw, labels = tf.train.batch([image, image_raw, label], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) return images, images_raw, labels
def __init__(self): slim = tf.contrib.slim CLASSES = ['anger', ' happy ', 'neutral', ' sad ', 'surprise'] image_size = 160 checkpoints_dir = '/root/catkin_ws/src/ros_emotion_detect/src/models/inception_5/' logging.basicConfig(filename='result.log', filemode='w', level=logging.INFO) self.logger = logging.getLogger('emotion classifier') # loading model with tf.Graph().as_default(): self.image = tf.placeholder(tf.uint8, [None, None, 3]) self.processed_image = inception_preprocessing.preprocess_image( self.image, image_size, image_size, is_training=False) self.processed_images = tf.placeholder( tf.float32, [None, image_size, image_size, 3]) with slim.arg_scope(resnet_v2.resnet_arg_scope()): logits, _ = resnet_v2.resnet_v2_50(self.processed_images, num_classes=len(CLASSES), is_training=False) self.probs = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'model.ckpt-60000'), slim.get_model_variables('resnet_v2_50')) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True self.sess = tf.Session(config=config) init_fn(self.sess)
def preprocessing_fn(image, output_height, output_width, **kwargs): return inception_preprocessing.preprocess_image( image, output_height, output_width, is_training=is_training, **kwargs)
def preprocess_fn(path): image = tf.read_file(path) image = tf.image.decode_jpeg(image, channels=3) image = inception_preprocessing.preprocess_image(image, FLAGS.width, FLAGS.height, is_training=False) return image
def classify_single_image(self, dataset_path, image_name): with self.graph.as_default(): image_raw = os.path.join(dataset_path, image_name) preprossed_image = inception_preprocessing.preprocess_image(image_raw, self.image_size, self.image_size, is_training=False) return self.session.run(self.probabilities, feed_dict={self.processed_images: preprossed_image})
def _parse_function(filename, label=None, is_training=True): image_string = tf.read_file(filename) image_decoded = tf.image.decode_jpeg(image_string, channels=3) images = inception_preprocessing.preprocess_image(image_decoded, height=224, width=224, is_training=is_training) return images, label
def load_batch(self, dataset, batch_size=None, is_training=None): """Loads a single batch of data. Args: dataset: The dataset to load. batch_size: The number of images in the batch. height: The size of each image after preprocessing. width: The size of each image after preprocessing. is_training: Whether or not we're currently training or evaluating. Returns: images: A Tensor of size [batch_size, height, width, 3], image samples that have been preprocessed. images_raw: A Tensor of size [batch_size, height, width, 3], image samples that can be used for visualization. labels: A Tensor of size [batch_size], whose values range between 0 and dataset.num_classes. """ shuffle = False num_readers = 1 if is_training: shuffle = True #make sure most samples can be fetched in one epoch num_readers = 1 data_provider = slim.dataset_data_provider.DatasetDataProvider( dataset, shuffle=shuffle, num_readers=num_readers, common_queue_capacity=20 * batch_size, common_queue_min=10 * batch_size) # data_provider = slim.dataset_data_provider.DatasetDataProvider( # dataset, common_queue_capacity=32, # common_queue_min=8) image_raw, label, filename = data_provider.get( ['image', 'label', 'filename']) # Preprocess image for usage by Inception. #No image augmentation for now, but resize normalization image_size = inception_v4.inception_v4.default_image_size image = inception_preprocessing.preprocess_image(image_raw, image_size, image_size, is_training=False) # Preprocess the image for display purposes. image_raw = tf.expand_dims(image_raw, 0) image_raw = tf.image.resize_images(image_raw, [image_size, image_size]) image_raw = tf.squeeze(image_raw) # Batch it up. images, images_raw, labels, filenames = tf.train.batch( [image, image_raw, label, filename], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) tf.summary.image('image_raw', images_raw) tf.summary.image('image', images) return images, images_raw, labels, filenames
def run(args): data_path = args.data_path label_path = args.label_path model_path = args.model_path model_name = args.model_name model_scope = model_name + '_arg_scope' inception = importlib.import_module('nets.' + model_name) with tf.Graph().as_default(): with slim.arg_scope(getattr(inception, model_scope)()): files = glob.glob(data_path + os.path.sep + "*.jpg") file_list = list() for idx, f in enumerate(files): f_string = tf.gfile.FastGFile(f, 'rb').read() test_img = tf.image.decode_jpeg(f_string, channels=3) processed_image = inception_preprocessing.preprocess_image( test_img, image_size, image_size, is_training=False) #processed_images = tf.expand_dims(processed_image, 0) file_list.append(os.path.basename(f)) if (idx == 0): processed_images = [processed_image] else: processed_images.append(processed_image) processed_images = tf.stack(processed_images, axis=0) with open(label_path, 'r') as rdata: names = dict() for row in rdata: strip_row = row.strip() split_row = strip_row.split(":") if (len(split_row) == 2): names[int(split_row[0])] = split_row[1] print(names) logits, _ = getattr(inception, model_name)(processed_images, num_classes=14, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( model_path, slim.get_model_variables('InceptionV3')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run( [processed_images, probabilities]) print("\n======== DATA RESULT =======\n") print("filename\t" + "\t".join(names.values())) for idx, iter in enumerate(probabilities): print(file_list[idx] + '\t' + '\t'.join([str(round(i, 2)) for i in iter]))
def input_parser(img_path): # # read the img from file img_file = tf.read_file(img_path) img_decoded = tf.image.decode_png(img_file,channels=3) img_decoded=tf.image.convert_image_dtype(img_decoded,dtype=tf.float32) processed_image = inception_preprocessing.preprocess_image(img_decoded, image_size, image_size, is_training=False) return processed_image
def classify(checkpoints_dir, images,logo_names=[""], reuse=False): image_size = inception.inception_v4.default_image_size probabilities_list = [] processed_image_list = [] images_list = [] output_probabilities = [] for image in images: image = tf.image.decode_jpeg(image, channels=3) #image = tf.image.resize_image_with_crop_or_pad(image, 299, 299) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) #processed_images = tf.expand_dims(processed_image, 0) images_list.append(image) processed_image_list.append(processed_image) with slim.arg_scope(inception_utils.inception_arg_scope()): logits, _ = inception.inception_v4(processed_image_list, #num_classes=2, reuse=reuse, is_training=False, logo_names= logo_names) probabilities = [] output_probabilities = [] for logo_name in logo_names: probabilities.append(tf.nn.softmax(logits[logo_name])) if tf.gfile.IsDirectory(checkpoints_dir): checkpoints_dir = tf.train.latest_checkpoint(checkpoints_dir) init_fn = slim.assign_from_checkpoint_fn( checkpoints_dir, slim.get_model_variables('InceptionV4'), ignore_missing_vars=True) with tf.Session() as sess: init_fn(sess) output_probabilities = sess.run([images_list, processed_image_list] + probabilities)[2:] output_dict = {} print "range(len(output_probabilities): ", range(len(output_probabilities)) for index in range(len(output_probabilities)): print 'logo_names[index]:[{}]'.format(logo_names[index]) print('type logo_names[index]',type(logo_names[index])); if logo_names[index] == "": print "here???????????????????" output_dict [logo_names[index]] = output_probabilities[index] else: output_dict [logo_names[index]] = output_probabilities[index] print "output_dict [""]: ", output_dict [""].shape print "final 0,780 prob output_dict [""]: ", output_dict[""][0][780]; output_dict[""] = np.argsort(output_dict[""], axis=1)[:, ::-1][:, :5] print "final output_dict [""]: ", output_dict [""] return output_dict
def get_Image_Batch_From_Tfrecord_1(filenames, batch_size=FLAGS.batch_size, shuffle=True, enqueue_many=False): filenameQ = tf.train.string_input_producer(filenames, num_epochs=None) recordReader = tf.TFRecordReader() key, fullExample = recordReader.read(filenameQ) features = tf.parse_single_example( fullExample, features={ 'image/height': tf.FixedLenFeature([], tf.int64), 'image/width': tf.FixedLenFeature([], tf.int64), 'image/colorspace': tf.FixedLenFeature([], dtype=tf.string, default_value=''), #'image/channels': tf.FixedLenFeature([], tf.int64), 'image/class/label': tf.FixedLenFeature([], tf.int64), 'image/class/text': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/format': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/filename': tf.FixedLenFeature([], dtype=tf.string, default_value=''), 'image/encoded': tf.FixedLenFeature([], dtype=tf.string, default_value='') }) label = features['image/class/label'] image_buffer = features['image/encoded'] with tf.name_scope('decode_png', [image_buffer], None): image = tf.image.decode_png(image_buffer, channels=NCHANNEL) #image = tf.image.convert_image_dtype(image, dtype=tf.float32) pdb.set_trace() image = inception_preprocessing.preprocess_image(image, 299, 299, is_training=False) # label = tf.stack(tf.one_hot(label - 1, NCLASS)) if shuffle: imageBatch, labelBatch = tf.train.shuffle_batch( [image, label], batch_size=batch_size, capacity=2 * batch_size, min_after_dequeue=1, enqueue_many=enqueue_many) else: imageBatch, labelBatch = tf.train.batch([image, label], batch_size=batch_size, num_threads=1, capacity=2 * batch_size) return imageBatch, labelBatch
def predict(self, img_file): ''' 对给定的图片进行预测,给出Top5类别的名称及可能的概率 img_file:全路径文件名 ''' print('image file:{0}'.format(img_file)) names = self.get_imagenet_label_names() with tf.Graph().as_default(): with self.slim.arg_scope( inception_resnet_v2.inception_resnet_v2_arg_scope()): testImage_string = tf.gfile.FastGFile(img_file, 'rb').read() testImage = tf.image.decode_jpeg(testImage_string, channels=3) processed_image = inception_preprocessing.preprocess_image( testImage, self.image_size, self.image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) logits, _ = inception_resnet_v2.inception_resnet_v2( processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = self.slim.assign_from_checkpoint_fn( os.path.join(self.ckpt_dir, 'inception_resnet_v2_2016_08_30.ckpt'), self.slim.get_model_variables('InceptionResnetV2')) with tf.Session() as sess: init_fn(sess) np_image, probabilities = sess.run( [processed_images, probabilities]) probabilities = probabilities[0, 0:] sorted_inds = [ i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1]) ] #names = imagenet.create_readable_names_for_imagenet_labels() for i in range(5): index = sorted_inds[i] print((probabilities[index], names[index])) print('show:{0}'.format(img_file)) img = np.array(Image.open(img_file)) plt.rcParams['font.sans-serif'] = [ 'SimHei' ] # 在图片上显示中文,如果直接显示形式为:u'内容' plt.rcParams['axes.unicode_minus'] = False # 显示-号 plt.figure() plt.imshow(img) myfont = FontProperties( fname='/usr/share/fonts/truetype/arphic/uming.ttc') plt.suptitle(names[sorted_inds[0]], fontsize=14, fontweight='bold', fontproperties=myfont) plt.axis('off') plt.show()
def transform_image_for_inception(imageFilePath): image_size = inception.inception_v3.default_image_size out = [] image_raw = tf.image.decode_png(open(imageFilePath, 'rb').read(), channels=3) image = inception_preprocessing.preprocess_image(image_raw, image_size, image_size, is_training=False) out.append(image) return session.run([out])[0][0]
def main(_): if not FLAGS.output_file: raise ValueError( 'You must supply the path to save to with --output_file') if FLAGS.is_video_model and not FLAGS.num_frames: raise ValueError( 'Number of frames must be specified for video models with --num_frames' ) tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default() as graph: dataset = dataset_factory.get_dataset(FLAGS.dataset_name, 'train', FLAGS.dataset_dir) network_fn = nets_factory.get_network_fn( FLAGS.model_name, #num_classes=(dataset.num_classes - FLAGS.labels_offset), num_classes=3, is_training=FLAGS.is_training) image_size = FLAGS.image_size or network_fn.default_image_size if FLAGS.is_video_model: input_shape = [ FLAGS.batch_size, FLAGS.num_frames, image_size, image_size, 3 ] else: input_shape = [FLAGS.batch_size, image_size, image_size, 3] #placeholder = tf.placeholder(name='input', dtype=tf.float32, # shape=input_shape) """여기서부터 코드추가한부분""" with tf.name_scope('image_preprocessing'): input_bytes = tf.placeholder(tf.string, shape=[], name="input_bytes") input_bytes = tf.reshape(input_bytes, []) input_tensor = tf.image.decode_jpeg(input_bytes, channels=3) processed_image = inception_preprocessing.preprocess_image( input_tensor, 299, 299, is_training=False) processed_image = tf.reshape(processed_image, [-1, 299, 299, 3]) """여기까지 코드추가한부분""" network_fn(processed_image) if FLAGS.quantize: tf.contrib.quantize.create_eval_graph() graph_def = graph.as_graph_def() if FLAGS.write_text_graphdef: tf.io.write_graph(graph_def, os.path.dirname(FLAGS.output_file), os.path.basename(FLAGS.output_file), as_text=True) else: with gfile.GFile(FLAGS.output_file, 'wb') as f: f.write(graph_def.SerializeToString())
def read_tensor_from_image_file(file_name, input_height=224, input_width=224): image_string = tf.gfile.FastGFile(file_name, 'rb').read() image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, input_height, input_width, is_training=False) processed_images = tf.expand_dims(processed_image, 0) sess = tf.Session() result = sess.run(processed_images) print(result.shape) return result
from datasets import imagenet from nets import inception from preprocessing import inception_preprocessing slim = tf.contrib.slim image_size = inception.inception_v1.default_image_size with tf.Graph().as_default(): #url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg' #url = 'https://pbs.twimg.com/media/B5B9ZsvIgAATiC6.jpg' url=sys.argv[1] image_string = urllib2.urlopen(url).read() image = tf.image.decode_jpeg(image_string, channels=3) processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False) processed_images = tf.expand_dims(processed_image, 0) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v1_arg_scope()): logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) checkpoints_dir='slim_pretrained' init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v1.ckpt'), slim.get_variables_to_restore()) #slim.get_model_variables()) #slim.get_model_variables('InceptionV1')) #saver= tf.train.Saver(tf.all_variables()) #saver= tf.train.Saver(list(set(slim.get_model_variables()) | set(tf.trainable_variables()))) with tf.Session() as sess: