def Pred(): global result, label image = cv2.imread(filepath) print("[INFO] loading and preprocessing image...") image = image_utils.load_img(filepath, target_size=(224, 224)) image = image_utils.img_to_array(image) image = np.expand_dims(image, axis=0) start = timeit.default_timer() # load the network print("[INFO] loading network...") model, tags_from_model = net.load("model") net.compile(model) # classify the image print("[INFO] classifying image...") preds = model.predict(image) y_classes = probas_to_classes(preds) if (y_classes == 1): print "it is cancerous" result = "malignant" else: print "it is benign" result = "benign" label.config(text=result) stop = timeit.default_timer() exec_time = stop - start print "Predicting Execution Time is %f s" % exec_time return
def get_weights(model_path): model, tags = net.load(model_path) net.compile(model) weights_map = {} for layer in model.layers: weights = layer.get_weights() weights_map[layer.name] = weights return weights_map
def get_layers(model_path): model, tags = net.load(model_path) net.compile(model) print len(model.layers) count = 0 for layer in model.layers: print count + "," + layer.get_config()["name"] count += 1
def predict(model_path, dataset_dir): model, tags = net.load(model_path) data_X, data_y, tags = dataset.dataset(dataset_dir, 299, False) net.compile(model) predictions = [] for d in data_X: X = np.expand_dims(d, axis=0) prediction = model.predict(X) predictions.append(prediction[0]) return predictions
def predict_with_dataset(model_path, dataset): model, tags = net.load(model_path) data_X = dataset.X data_y = dataset.y net.compile(model) predictions = [] for d in data_X: X = np.expand_dims(d, axis=0) prediction = model.predict(X) predictions.append(prediction[0]) return predictions
def __init__( self, model_path, memory_location='../mainstream-analysis/output/mainstream/predictions' ): super(Model, self).__init__() self.model, self.tags = net.load(model_path) net.compile(self.model) self.dim = 224 if 'mobilenets' in model_path else 299 print 'Tags', self.tags self.memory = {} if not os.path.isdir(memory_location): os.mkdir(memory_location) assert os.path.isdir(memory_location) self.memory_location = os.path.join(memory_location, os.path.basename(model_path)) if not os.path.isdir(self.memory_location): os.mkdir(self.memory_location)
def predict(model_path, dataset_dir): model, tags = net.load(model_path) print tags # Inception # data_X, data_y, tags = dataset.dataset(dataset_dir, 299, False) # MobileNets data_X, data_y, tags = dataset.dataset(dataset_dir, 224, False) net.compile(model) predictions = [] # print 'shape', data_X.shape # prediction = model.predict(data_X) # print prediction.shape # prediction = prediction[:, 0] for d in data_X: X = np.expand_dims(d, axis=0) prediction = model.predict(X) predictions.append(prediction[0]) return predictions
def save_imagenet(net_architecture, prefix): K.set_learning_phase(1) # Set neural net architecture if net_architecture == "InceptionV3": model = InceptionV3(weights="imagenet", include_top=True) elif net_architecture == "ResNet50": model = ResNet50(weights="imagenet", include_top=True) elif net_architecture == "MobileNet": model = MobileNet(weights="imagenet", include_top=True) else: print "[ERROR] Didn't recognize net ", net_architecture sys.exit(-1) net.compile(model) net.save_pb(model, prefix) freeze.freeze(prefix) return
def predict_by_tag(model_path, dataset_dir, tag): model, tags = net.load(model_path) tag_index = [i for i, t in enumerate(tags) if t == tag][0] # Inception # data_X = dataset.dataset_with_root_dir(dataset_dir, 299) # MobileNets data_X = dataset.dataset_with_root_dir(dataset_dir, 224) print tags net.compile(model) predictions = [] for d in data_X: X = np.expand_dims(d, axis=0) prediction = (model.predict(X)).tolist()[0] argmax = prediction.index(max(prediction)) if argmax == tag_index: predictions.append(1) else: predictions.append(0) return predictions
vis_image = 255 * np.ones( (vis_image_size, vis_image_size, 3), dtype='uint8') example_counts = defaultdict(int) for (predicted_tag, actual_tag, normalized_image) in zip(y_pred, y_test, X_test): example_count = example_counts[(predicted_tag, actual_tag)] if example_count >= bucket_size**2: continue image = dataset.reverse_preprocess_input(normalized_image) image = image.transpose((1, 2, 0)) image = scipy.misc.imresize( image, (image_size, image_size)).astype(np.uint8) tilepos_x = bucket_size * predicted_tag tilepos_y = bucket_size * actual_tag tilepos_x += example_count % bucket_size tilepos_y += example_count // bucket_size pos_x, pos_y = tilepos_x * image_size, tilepos_y * image_size vis_image[pos_y:pos_y + image_size, pos_x:pos_x + image_size, :] = image example_counts[(predicted_tag, actual_tag)] += 1 vis_image[::image_size * bucket_size, :] = 0 vis_image[:, ::image_size * bucket_size] = 0 scipy.misc.imsave(vis_filename, vis_image) model, tags_from_model = net.load("model") assert tags == tags_from_model net.compile(model) evaluate(model, "classifier.png")
if vis_filename is not None: bucket_size = 10 image_size = n // 4 # right now that's 56 vis_image_size = nb_classes * image_size * bucket_size vis_image = 255 * np.ones((vis_image_size, vis_image_size, 3), dtype='uint8') example_counts = defaultdict(int) for (predicted_tag, actual_tag, normalized_image) in zip(y_pred, y_test, X_test): example_count = example_counts[(predicted_tag, actual_tag)] if example_count >= bucket_size**2: continue image = dataset.reverse_preprocess_input(normalized_image) image = image.transpose((1, 2, 0)) image = scipy.misc.imresize(image, (image_size, image_size)).astype(np.uint8) tilepos_x = bucket_size * predicted_tag tilepos_y = bucket_size * actual_tag tilepos_x += example_count % bucket_size tilepos_y += example_count // bucket_size pos_x, pos_y = tilepos_x * image_size, tilepos_y * image_size vis_image[pos_y:pos_y+image_size, pos_x:pos_x+image_size, :] = image example_counts[(predicted_tag, actual_tag)] += 1 vis_image[::image_size * bucket_size, :] = 0 vis_image[:, ::image_size * bucket_size] = 0 scipy.misc.imsave(vis_filename, vis_image) model, tags_from_model = net.load("model") assert tags == tags_from_model net.compile(model) evaluate(model, "classifier.png")