def __init__(self, network_name, init_fn, taylor=True): if network_name == 'InceptionV1': shift_index = False layer_names = inception_names traverse_graph = traverse.inception_v1 elif network_name == 'vgg_16': shift_index = True layer_names = vgg_16_names traverse_graph = traverse.vgg_16 else: raise Exception('Unkown network name: ' + str(network_name)) self.init_fn = init_fn self.sess_config = tf.ConfigProto(device_count={'GPU': 0}) self.input_image = tf.get_default_graph().get_tensor_by_name( 'input_layer:0') self.output_layer = tf.get_default_graph().get_tensor_by_name( 'probabilities:0') self.traverse_graph = traverse_graph self.shift_index = shift_index self.imagenet_labels = imagenet.create_readable_names_for_imagenet_labels( ) self.layer_names = layer_names self.taylor = Taylor(self.input_image, self.init_fn, self.sess_config, self.traverse_graph, self.output_layer)
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 recognize(jpg_path, pb_file_path): with tf.Graph().as_default(): graph, output_tensor, input_tensor = load_graph(pb_file_path) with tf.Session(graph=graph) as sess: # # get the input tensor operation # input_x = graph.get_tensor_by_name("import/DecodeJpeg/contents:0") # # get the output tensor operation # output = graph.get_tensor_by_name("import/Softmax:0") # read the image image_data = gfile.FastGFile(jpg_path, 'rb').read() t1 = time.time() pre = sess.run(output_tensor, feed_dict={input_tensor: image_data}) t2 = time.time() writer = tf.summary.FileWriter("./logs_from_pb", graph=tf.get_default_graph()) results = np.squeeze(pre) prediction_labels = np.argmax(results, axis=0) names = imagenet.create_readable_names_for_imagenet_labels() top_k = results.argsort()[-5:][::-1] for i in top_k: print(names[i + 1], results[i]) print('probability: %s: %.3g, running time: %.3g' % (names[prediction_labels + 1], results[prediction_labels], t2 - t1))
def __init__(self, checkpoint='../mobilenet_v2_1.0_224.ckpt'): # save the checkpoint self.checkpoint = checkpoint tf.reset_default_graph() # placeholder for the image input, need to decode the file self.file_in = tf.placeholder(tf.string, ()) image = tf.image.decode_jpeg(tf.read_file(self.file_in)) # expand for batch then cast to between -1 and 1 inputs = tf.expand_dims(image, 0) inputs = (tf.cast(inputs, tf.float32) / 128) - 1 # ensure that it only has three dimensions and resize to 224x224 inputs.set_shape((None, None, None, 3)) inputs = tf.image.resize_images(inputs, (224, 224)) # get the endpoints of the network with tf.contrib.slim.arg_scope( mobilenet_v2.training_scope(is_training=False)): _, self.endpoints = mobilenet_v2.mobilenet(inputs) # Restore using exponential moving average since it produces (1.5-2%) higher # accuracy ema = tf.train.ExponentialMovingAverage(0.999) vars = ema.variables_to_restore() saver = tf.train.Saver(vars) # create the label map from imagenet, same thing self.label_map = imagenet.create_readable_names_for_imagenet_labels() # create session and restore the checkpoint downloaded self.sess = tf.Session() saver.restore(self.sess, self.checkpoint)
def main(): cap = cv2.VideoCapture('./test_videos/20180619_175221224.mp4') #cap = cv2.VideoCapture('./test_videos/Formula Student Spain 2015 Endurance- DHBW Engineering with the eSleek15.mp4') frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) fps = int(cap.get(cv2.CAP_PROP_FPS)) count = 0 gd = tf.GraphDef.FromString( open('./frozen_weights/resnetv2_imagenet_frozen_graph.pb', 'rb').read()) inp, predictions = tf.import_graph_def( gd, return_elements=['input:0', 'MobilenetV2/Predictions/Reshape_1:0']) with tf.Session(graph=inp.graph): while count < frameCount: ret, image_np = cap.read() if ret == True: count = count + 1 img = cv2.resize(image_np, (224, 224)) x = predictions.eval( feed_dict={inp: img.reshape(1, 224, 224, 3)}) label_map = imagenet.create_readable_names_for_imagenet_labels( ) print("Top 1 Prediction: ", x.argmax(), label_map[x.argmax()], x.max())
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 select_top_label(): imagenet_labels = {} label_names = imagenet.create_readable_names_for_imagenet_labels() label_names = {k: v.lower() for k, v in label_names.items()} for label_id in range(1, 1001): names = label_names[label_id] for name in names.split(','): name = name.strip() label = name.split()[-1] if label not in imagenet_labels: imagenet_labels[label] = [] imagenet_labels[label].append(names) fin = open(sample_file) label_count = {} while True: line = fin.readline().strip() if not line: break fields = line.split(FIELD_SEPERATOR) labels = fields[LABEL_INDEX] labels = labels.split(LABEL_SEPERATOR) for label in labels: label_count[label] = label_count.get(label, 0) + 1 fin.close() invalid_labels = ['bank', 'center', 'home', 'jack', 'maria'] invalid_labels.append('apple') # custard apple invalid_labels.append('bar') # horizontal bar, high bar invalid_labels.append('coral') # brain coral invalid_labels.append('dragon') # komodo dragon invalid_labels.append('grand') # grand piano invalid_labels.append('star') # starfish, sea star invalid_labels.append('track') # half track label_count = sorted(label_count.items(), key=operator.itemgetter(1, 0), reverse=True) top_labels, num_label, = set(), 0 for label, _ in label_count: if num_label == NUM_TOP_LABEL: break if label in invalid_labels: continue if label not in imagenet_labels: continue top_labels.add(label) num_label += 1 top_labels = sorted(top_labels) for count, label in enumerate(top_labels): names = [] for label_id in range(1, 1001): if label in label_names[label_id]: names.append(label_names[label_id]) print('#%d label=%s' % (count + 1, label)) for names in imagenet_labels[label]: print('\t%s' % (names)) # input() utils.save_collection(top_labels, label_file)
def display_imagenet_prediction(image, class_index): imagenet_classnames = imagenet.create_readable_names_for_imagenet_labels() class_label = imagenet_classnames[class_index] print("Prediction: {} (class_index={})".format(class_label, class_index)) plt.figure() plt.imshow(image) plt.axis('off') plt.show()
def __init__(self, model_url, n_labels=5): ''' init Model, checkpoints dir, model properties''' self.checkpoints_dir = os.path.join(os.path.dirname(__file__), 'checkpoints') self.slim = tf.contrib.slim self.image_size = vgg.vgg_16.default_image_size self.names = imagenet.create_readable_names_for_imagenet_labels() self.n_labels = n_labels
def predict(image_filename): sess, graph, endpoints, file_input = getMobileNet(checkpoint) label_map = imagenet.create_readable_names_for_imagenet_labels() x = endpoints['Predictions'].eval(session=sess, feed_dict={file_input: image_filename}) argmax = x.argsort()[:, -5:][:, ::-1].tolist()[0] res = {} for i, index in enumerate(argmax): res['top{}'.format(i+1)] = {'label': label_map[index], 'confidence': '{:.4f}'.format(x[0][index])} return res
def tst1(): names = imagenet.create_readable_names_for_imagenet_labels() print(type(names)) print(len(names.keys())) n = 0 for k, v in names.items(): print('{}: {}'.format(k, v)) n += 1 if (n == 3): break
def create_label_json_file(json_fn): labels = imagenet.create_readable_names_for_imagenet_labels() with open(json_fn, 'w') as ofp: json.dump(labels, ofp, sort_keys=True, indent=4, separators=(',', ': ')) return labels
def create_label_json_file(json_fn): labels = imagenet.create_readable_names_for_imagenet_labels() with open(json_fn, 'w') as ofp: json.dump(labels, ofp, sort_keys=True, indent=4, separators=(',', ': ')) return labels
def predict(image, version='V3'): tf.reset_default_graph() # Process the image raw_image, processed_image = process_image(image) class_names = imagenet.create_readable_names_for_imagenet_labels() # Create a placeholder for the images X = tf.placeholder(tf.float32, [None, 299, 299, 3], name="X") #inception_v3 function returns logits and end_points dictionary #logits are output of the network before applying softmax activation if version.upper() == 'V3': model_ckpt_path = INCEPTION_V3_CKPT_PATH with slim.arg_scope(inception.inception_v3_arg_scope()): # Set the number of classes and is_training parameter logits, end_points = inception.inception_v3( X, num_classes=1001, is_training=False) elif version.upper() == 'V4': model_ckpt_path = INCEPTION_V4_CKPT_PATH with slim.arg_scope(inception.inception_v3_arg_scope()): # Set the number of classes and is_training parameter # Logits logits, end_points = inception.inception_v4( X, num_classes=1001, is_training=False) predictions = end_points.get('Predictions', 'No key named predictions') saver = tf.train.Saver() with tf.Session() as sess: saver.restore(sess, model_ckpt_path) prediction_values = predictions.eval({X: processed_image}) try: # Add an index to predictions and then sort by probability prediction_values = [ (i, prediction) for i, prediction in enumerate(prediction_values[0, :]) ] prediction_values = sorted( prediction_values, key=lambda x: x[1], reverse=True) # Plot the image # plot_color_image(raw_image) plot_color_image(image) print("Using Inception_{} CNN\nPrediction: Probability\n".format( version)) # Display the image and predictions for i in range(5): predicted_class = class_names[prediction_values[i][0]] probability = prediction_values[i][1] print("{}: {:.2f}%".format(predicted_class, probability * 100)) # If the predictions do not come out right except: print(predictions)
def main(_): if not FLAGS.test_path: raise ValueError('You must supply the test list with --test_path') tf.logging.set_verbosity(tf.logging.INFO) with tf.Graph().as_default(): # tf_global_step = slim.get_or_create_global_step() #################### # Select the model # #################### network_fn = nets_factory.get_network_fn( FLAGS.model_name, num_classes=(FLAGS.num_classes - FLAGS.labels_offset), is_training=False) ##################################### # Select the preprocessing function # ##################################### preprocessing_name = FLAGS.preprocessing_name or FLAGS.model_name image_preprocessing_fn = preprocessing_factory.get_preprocessing( preprocessing_name, is_training=False) test_image_size = FLAGS.test_image_size or network_fn.default_image_size if tf.gfile.IsDirectory(FLAGS.checkpoint_path): checkpoint_path = tf.train.latest_checkpoint(FLAGS.checkpoint_path) else: checkpoint_path = FLAGS.checkpoint_path print("restore from", checkpoint_path) tf.Graph().as_default() with tf.Session() as sess: image = open(FLAGS.test_path, 'rb').read() image = tf.image.decode_jpeg(image, channels=3) processed_image = image_preprocessing_fn(image, test_image_size, test_image_size) processed_images = tf.expand_dims(processed_image, 0) logits, _ = network_fn(processed_images) probabilities = tf.nn.softmax(logits) saver = tf.train.Saver() saver.restore(sess, checkpoint_path) np_image, network_input, predictions = sess.run( [image, processed_image, probabilities]) probabilities = np.squeeze(predictions, 0) names = imagenet.create_readable_names_for_imagenet_labels() pre = np.argmax(probabilities, axis=0) print('{} {} {}'.format(FLAGS.test_path, pre, names[pre + 1])) top_k = probabilities.argsort()[-5:][::-1] for index in top_k: print('Probability %0.2f => [%s]' % (probabilities[index], names[index + 1]))
async def process_image(image_path): image_size = vgg.vgg_16.default_image_size with tf.Graph().as_default(): # Convert filepath string to string tensor #tf_filepath = tf.convert_to_tensor(image_path, dtype=tf.string) #tf_filepath = tf.convert_to_tensor(str(image_path), dtype=tf.string) # Read .JPEG image #tf_img_string = tf.read_file(tf_filepath) image = tf.image.decode_jpeg(tf.image.encode_jpeg(image_path), channels=3) tf_img_string = tf.read_file(str(image_path)) image = tf.image.decode_jpeg( tf_img_string) #tf.image.encode_jpeg(tf_img_string), channels=3) processed_image = vgg_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(vgg.vgg_arg_scope()): # 1000 classes instead of 1001. logits, _ = vgg.vgg_16(processed_images, num_classes=1000, is_training=False) probabilities = tf.nn.softmax(logits) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'vgg_16.ckpt'), slim.get_model_variables('vgg_16')) with tf.Session() as sess: 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]) ] names = imagenet.create_readable_names_for_imagenet_labels() animals_found = [] for i in range(5): index = sorted_inds[i] # Shift the index of a class name by one. # print('Probability %0.2f%% => [%s]' % (probabilities[index] * 100, names[index+1])) animals_found.append(names[index + 1]) return animals_found
def test_image_data(): slim = tf.contrib.slim tf.reset_default_graph() session = tf.Session() names = imagenet.create_readable_names_for_imagenet_labels() processed_images = tf.placeholder(tf.float32, shape=(None, 299, 299, 3)) with slim.arg_scope(inception.inception_v3_arg_scope()): logits, _ = inception.inception_v3(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) # Please correctly set the model path. # Download the model at https://github.com/tensorflow/models/tree/master/research/slim checkpoints_dir = 'model' init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v3.ckpt'), slim.get_model_variables('InceptionV3')) init_fn(session) def predict_fn(images): return session.run(probabilities, feed_dict={processed_images: images}) def f(x): return x / 2 + 0.5 class_names = [] for item in names: class_names.append(names[item]) images = transform_img_fn(['data/violin.JPEG']) image = images[0] explainer = xdeep_image.ImageExplainer(predict_fn, class_names) explainer.explain('lime', image, top_labels=3) explainer.show_explanation('lime', deprocess=f, positive_only=False) explainer.explain('cle', image, top_labels=3) explainer.show_explanation('cle', deprocess=f, positive_only=False) explainer.explain('anchor', image) explainer.show_explanation('anchor') segments_slic = slic(image, n_segments=50, compactness=30, sigma=3) explainer.initialize_shap(n_segment=50, segment=segments_slic) explainer.explain('shap', image, nsamples=400) explainer.show_explanation('shap', deprocess=f)
def apply_pretrained_model(): import numpy as np import os import tensorflow as tf import urllib2 from datasets import imagenet from nets import inception from preprocessing import inception_preprocessing slim = tf.contrib.slim batch_size = 3 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' 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) 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) init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoint_dir, 'inception_v1.ckpt'), slim.get_model_variables('InceptionV1')) with tf.Session() as sess: 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('Probabity %0.2f%% => [%s]' % (probabilities[index], names[index]))
def __init__(self, sess, ckptFile): self.sess = sess self.inputs = tf.placeholder(shape=[None, 224, 224, 3], dtype=tf.float32) self.classes = imagenet.create_readable_names_for_imagenet_labels() with self.sess.as_default(): with tf.contrib.slim.arg_scope(inception.inception_v1_arg_scope()): self.logits, self.net = inception.inception_v1( self.inputs, 1001, is_training=False) self.probs = tf.nn.softmax(self.logits) initFn = slim.assign_from_checkpoint_fn( ckptFile, slim.get_model_variables("InceptionV1")) initFn(self.sess)
def load_pretrained_slim_model(): global session, names, probabilities, processed_images, slim_home_dir names = imagenet.create_readable_names_for_imagenet_labels() processed_images = tf.placeholder(tf.float32, shape=(None, 299, 299, 3)) with slim.arg_scope(inception.inception_v3_arg_scope()): logits, _ = inception.inception_v3(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) checkpoints_dir = os.path.join(slim_home_dir, 'pretrained') session = tf.Session() init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v3.ckpt'), slim.get_model_variables('InceptionV3')) init_fn(session)
def __init__(self): self.cv_bridge = CvBridge() self.model_path = rospy.get_param('~model_path', '/tmp/') self.model_file = self.model_path + 'inception_v1.ckpt' if (not os.path.exists(self.model_file)): rospy.logwarn( "Model files not present:\n\t{}\nWe will download them from tensorflow." .format(self.model_file)) from datasets import dataset_utils url = "http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz" dataset_utils.download_and_uncompress_tarball(url, self.model_path) self.names = imagenet.create_readable_names_for_imagenet_labels() s = rospy.Service('recognize', Roi, self.recognize)
def disp_names(self, sorted_inds, probabilities, include_background=True): dump_load = DumpLoad("../../data/imagenet/imagenet_labels_dict.pickle") if dump_load.isExisiting(): names = dump_load.load() else: names = imagenet.create_readable_names_for_imagenet_labels() dump_load.dump(names) for i in range(5): index = sorted_inds[i] if include_background: print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index])) else: print('Probability %0.2f%% => [%s]' % (probabilities[index], names[index + 1])) return
def inception_tst(): with tf.Graph().as_default(): url = 'https://upload.wikimedia.org/wikipedia/commons/7/70/EnglishCockerSpaniel_simon.jpg' 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_v1_arg_scope()): logits, _ = inception.inception_v1(processed_images, num_classes=1001, is_training=False) probabilities = tf.nn.softmax(logits) variables_to_restore = slim.get_variables() print(len(variables_to_restore)) # pprint(variables_to_restore) variables_to_restore = slim.get_model_variables() print(len(variables_to_restore)) return init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v1.ckpt'), slim.get_model_variables('InceptionV1')) with tf.Session() as sess: 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] * 100, names[index]))
def __init__(self): self.slim = tf.contrib.slim self.image_size = inception_v3.inception_v3.default_image_size self.checkpoints_dir = 'checkpoints' self.names = imagenet.create_readable_names_for_imagenet_labels() self.arg_scope = inception_v3.inception_v3_arg_scope() self.image = tf.placeholder(tf.uint8, [480, 640, 3]) #self.label = tf.placeholder(tf.int64, (1,)) self.type = None self.type_pred = None self.threshold = None self.ratio = None 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.cnet, self.middle, self.logits, self.end_points = inception_v3.inception_v3( 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_v3.ckpt'), self.slim.get_model_variables('InceptionV3')) self.slice_logits = tf.placeholder(tf.float32, (1, 1001)) self.slice_aux_logits = tf.placeholder(tf.float32, (1, 1001)) self.slice_labels = tf.placeholder(tf.int32, (1, 1)) self.loss = helper_function.inception_loss(self.slice_logits, self.slice_aux_logits, self.slice_labels) #self.mimage = tf.placeholder("float", self.cnet.get_shape()) #print('##################', self.middle) #print('@@@@@@@@@@@@@@@@@@', self.logits) self.session = tf.Session() self.init_fn(self.session) self.writer = tf.summary.FileWriter("output", self.session.graph) self.del_map = np.zeros(self.middle.get_shape().as_list())
def print_class(probs): probabilities = np.squeeze(probs) sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x: x[1])] plt.imshow(np.squeeze(network_input)) plt.suptitle("Resized, Cropped and Mean-Centered input to network", fontsize=14, fontweight='bold') plt.axis('off') names = imagenet.create_readable_names_for_imagenet_labels() for i in range(5): index = sorted_inds[i] # Now we print the top-5 predictions that the network gives us with # corresponding probabilities. Pay attention that the index with # class names is shifted by 1 -- this is because some networks # were trained on 1000 classes and others on 1001. VGG-16 was trained # on 1000 classes. print('Probability %0.2f => [%s]' % (probabilities[index], names[index])) plt.show() return None
def get_embeddings(instances): image_size = 299 names = imagenet.create_readable_names_for_imagenet_labels() 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 im_id in tqdm(instances.keys()): img = cv2.imread(instances[im_id]['path']) embedding_list = [] for b in instances[im_id]['detections']: x1 = int(b['bbox'][0]) x2 = int(b['bbox'][2]) y1 = int(b['bbox'][1]) y2 = int(b['bbox'][3]) if y2 - y1 >= 8 and x2 - x1 >= 8: patch = img[y1:y2, x1:x2, :] scaled_img, logit_vals, embedding = sess.run( [processed_image, logits, pool5], feed_dict={image: patch}) b['pool5_resnet_v2_101'] = embedding[0, 0, 0, :]
def __init__(self, name): super(Classifier, self).__init__(name) maybe_download_and_extract() import logging from logging.handlers import RotatingFileHandler file_handler = RotatingFileHandler(FLAGS.log, maxBytes=1024 * 1024 * 100, backupCount=20) file_handler.setLevel(logging.INFO) formatter = logging.Formatter( "%(asctime)s - %(name)s - %(levelname)s - %(message)s") file_handler.setFormatter(formatter) self.logger.addHandler(file_handler) self.names = imagenet.create_readable_names_for_imagenet_labels() self.image_size = inception.inception_v4.default_image_size self.image_str_placeholder = tf.placeholder(tf.string) image = tf.image.decode_jpeg(self.image_str_placeholder, channels=3) processed_image = inception_preprocessing.preprocess_image( image, self.image_size, self.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_v4_arg_scope()): logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False) self.probabilities = tf.nn.softmax(logits) dest_directory = FLAGS.model_dir init_fn = slim.assign_from_checkpoint_fn( os.path.join(dest_directory, 'inception_v4.ckpt'), slim.get_model_variables('InceptionV4')) self.sess = tf.Session() init_fn(self.sess)
image = tf.image.decode_jpeg(tf.read_file(file_input)) images = tf.expand_dims(image, 0) images = tf.cast(images, tf.float32) / 128. - 1 images.set_shape((None, None, None, 3)) images = tf.image.resize_images(images, (224, 224)) # images = tf.placeholder(tf.float32, (None, 224, 224, 3)) # Note: arg_scope is optional for inference. with tf.contrib.slim.arg_scope(mobilenet_v2.training_scope(is_training=False)): logits, endpoints = mobilenet_v2.mobilenet(images) # Restore using exponential moving average since it produces (1.5-2%) higher # accuracy ema = tf.train.ExponentialMovingAverage(0.999) vars = ema.variables_to_restore() saver = tf.train.Saver(vars) from datasets import imagenet with tf.Session() as sess: saver.restore(sess, checkpoint) x = endpoints['Predictions'].eval(feed_dict={file_input: 'imgs/dog.jpg'}) # writer = tf.summary.FileWriter("TensorBoard/", graph=sess.graph) # writer.close() label_map = imagenet.create_readable_names_for_imagenet_labels() print("Top 1 prediction: ", x.argmax(), label_map[x.argmax()], x.max())
def read_image(image_folder, debug=0): if not os.path.exists(image_folder): print("Error: Image folder does not exist:", image_folder) sys.exit(0) images_paths = sorted(glob.glob(os.path.join(image_folder + '/*'))) if not len(images_paths): print("No images found in folder") sys.exit(0) graph = tf.Graph() with graph.as_default(): # Create a placeholder to pass the input image img_tensor = tf.placeholder(tf.float32, shape=(len(images_paths), HEIGHT, WIDTH, CHANNELS)) # Scale the image inputs to {+1, -1} from 0 to 255 img_scaled = tf.scalar_mul((1.0 / 255), img_tensor) img_scaled = tf.subtract(img_scaled, 0.5) img_scaled = tf.multiply(img_scaled, 2.0) # load Graph definitions with slim.arg_scope(inception_resnet_v2_arg_scope()): logits, end_points = inception_resnet_v2(img_scaled, is_training=False) # predict the class predictions = end_points['Predictions'] #Pre-processing images_paths final_images = [] for image in images_paths: img = cv2.imread(image) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.resize(img, (WIDTH, HEIGHT)) final_images.append(img) # make the input size [BATCH, WIDTH, HEIGHT, CHANNELS] for the network final_images = np.squeeze(np.expand_dims(final_images, axis=0)) #for labels of imagenet sys.path.append(tf_path + 'models/research/slim') from datasets import imagenet # Inception resnet v2 model checkpoint_path = './checkpoints/' model_name = 'inception_resnet_v2_2016_08_30' checkpoint_file = checkpoint_path + model_name + '.ckpt' with tf.Session(graph=graph) as sess: saver = tf.train.Saver() saver.restore(sess, checkpoint_file) pred_prob = sess.run(predictions, feed_dict={img_tensor: final_images}) # Getting the top 5 classes of the imagenet database image_results = dict() for file_index, p in enumerate(pred_prob): sorted_inds = [ i[0] for i in sorted(enumerate(-p), key=lambda x: x[1]) ] names = imagenet.create_readable_names_for_imagenet_labels() probability_tag = p[sorted_inds[0]] name_tag = names[sorted_inds[0]] image_results[images_paths[file_index]] = (name_tag, probability_tag) if debug: print("\nResult:", images_paths[file_index]) for i in range(5): index = sorted_inds[i] print('{:.2%} => [{}]'.format(p[index], names[index])) return image_results
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: 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])] #saver.save(sess,'/home/sankit/Documents/cv-tricks/models/slim/my-saved/inception_v1.ckpt') #saver.save(sess,'inception_v1') names = imagenet.create_readable_names_for_imagenet_labels() result_text='' for i in range(5): index = sorted_inds[i] print('Probability %0.2f%% => [%s]' % (100*probabilities[index], names[index])) result_text+=str(names[sorted_inds[0]])+'=>'+str( "{0:.2f}".format(100*probabilities[sorted_inds[0]]))+'%\n' plt.figure(num=1,figsize=(8, 6), dpi=80) plt.imshow(np_image.astype(np.uint8)) plt.text(150,100,result_text,horizontalalignment='center', verticalalignment='center',fontsize=21,color='blue') #plt.text(200,600,result_text) plt.axis('off') plt.show()
def build_graph(cluster, image_url, return_list): prob_list = return_list num_workers = cluster.num_tasks('worker') # default picture for testing if image_url == None: image_url = "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7e/Bow_bow.jpg/800px-Bow_bow.jpg" image_string = urllib.urlopen(image_url).read() #image_string = tf.read_file("/home/philiptkd/Downloads/Dependency_Tree.png") # I lost internet image_size = inception.inception_v1_dist.default_image_size # shared done list, ready list, and image with tf.device("/job:ps/task:0"): done_list = tf.get_variable("done_list", [num_workers+1], tf.int32, tf.zeros_initializer) ready_list = tf.get_variable("ready_list", [num_workers], tf.int32, tf.zeros_initializer) with tf.device("/job:worker/task:0"): # image 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) shared_image = tf.Variable(processed_images, name="shared_image") #download the inception v1 checkpoint if we need to url = "http://download.tensorflow.org/models/inception_v1_2016_08_28.tar.gz" checkpoints_dir = '/tmp/checkpoints' if not tf.gfile.Exists(checkpoints_dir): tf.gfile.MakeDirs(checkpoints_dir) if not tf.gfile.Exists(checkpoints_dir+'/inception_v1_2016_08_28.tar.gz'): dataset_utils.download_and_uncompress_tarball(url, checkpoints_dir) # end download server = tf.train.Server(cluster, job_name="ps", task_index=0) sess = tf.Session(target=server.target) # Create the model, use the default arg scope to configure the batch norm parameters. with slim.arg_scope(inception.inception_v1_dist_arg_scope()): logits, _ = inception.inception_v1_dist(shared_image, num_workers, num_classes=1001, is_training=False, reuse=tf.AUTO_REUSE) probabilities = tf.nn.softmax(logits) # initialization function that uses saved parameters init_fn = slim.assign_from_checkpoint_fn( os.path.join(checkpoints_dir, 'inception_v1.ckpt'), slim.get_model_variables('InceptionV1')) sess.run(tf.initialize_variables([done_list, ready_list, shared_image])) # initialize variables that aren't model parameters init_fn(sess) # wait for workers to acknowledge variables have been initialized while sess.run(tf.reduce_sum(ready_list)) < num_workers: pass # do the thing print("before getting probs") run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE) run_metadata = tf.RunMetadata() np_image, probabilities = sess.run([shared_image, probabilities], options=run_options, run_metadata=run_metadata) print("after getting probs") # see who did what for device in run_metadata.step_stats.dev_stats: print(device.device) for node in device.node_stats: print(" ", node.node_name) # indicate that the ps task is done sess.run(tf.scatter_update(done_list, [0], 1)) # wait until all tasks are done num_done = 1 while num_done < num_workers+1: num_done = sess.run(tf.reduce_sum(done_list)) sess.close() 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] probability = 'Probability %0.2f%% => [%s]' % (probabilities[index] * 100, names[index]) prob_list.append(probability) print(probability)
sys.path.insert(0, nets_path) else: print('already add slim') import tensorflow as tf #引入头文件 from PIL import Image from matplotlib import pyplot as plt from nets.nasnet import pnasnet import numpy as np from datasets import imagenet slim = tf.contrib.slim tf.reset_default_graph() image_size = pnasnet.build_pnasnet_large.default_image_size #获得图片输入尺寸 labels = imagenet.create_readable_names_for_imagenet_labels() #获得数据集标签 print(len(labels), labels) #显示输出标签 def getone(onestr): return onestr.replace(',', ' ') with open('中文标签.csv', 'r+') as f: #打开文件 labels = list(map(getone, list(f))) print(len(labels), type(labels), labels[:5]) #显示输出中文标签 sample_images = ['hy.jpg', 'ps.jpg', '72.jpg'] #定义待测试图片路径 input_imgs = tf.placeholder(tf.float32, [None, image_size, image_size, 3]) #定义占位符