我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用keras.preprocessing.image.img_to_array()。
def testTHPrediction(self): keras.backend.set_image_dim_ordering('th') model = SqueezeNet() img = image.load_img('images/cat.jpeg', target_size=(227, 227)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) decoded_preds = decode_predictions(preds) #print('Predicted:', decoded_preds) self.assertIn(decoded_preds[0][0][1], 'tabby') #self.assertAlmostEqual(decode_predictions(preds)[0][0][2], 0.82134342)
def extract(self, image_path): img = image.load_img(image_path, target_size=(299, 299)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # Get the prediction. features = self.model.predict(x) if self.weights is None: # For imagenet/default network: features = features[0] else: # For loaded network: features = features[0] return features
def ext_img_feat(image_folder, batch_size): base_model = ResNet50(weights='imagenet') img_model = Model(input=base_model.input, output=base_model.get_layer('res5c').output) img_list = os.listdir(image_folder) all_img_feats = list() si = 0 while si < len(img_list): batch_img = img_list[si:si+batch_size] si += batch_size imgs = [] for imgf in batch_img: img_path = os.path.join(image_folder, imgf) img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) imgs.append(x) imgs = np.concatenate(imgs, axis=0) img_feats = img_model.predict(imgs) all_img_feats.append(img_feats) print('%d images extracted\r'%si),
def extract_feature(dir_path, net): features = [] infos = [] num = 0 for image_name in os.listdir(dir_path): arr = image_name.split('_') person = int(arr[0]) camera = int(arr[1][1]) image_path = os.path.join(dir_path, image_name) img = image.load_img(image_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) feature = net.predict(x) features.append(np.squeeze(feature)) infos.append((person, camera)) return features, infos # use GPU to calculate the similarity matrix
def data_augmentation(image_files, dir): image_list = [] new_file_name = dir save_dir = "xxx" + new_file_name for image_file in image_files: image_list.append(misc.imread(image_file)) for image in image_list: x = img_to_array(image) # this is a Numpy array with shape (3, 150, 150) x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150) i = 0 for batch in datagen.flow(x, batch_size=1, save_to_dir=save_dir, save_prefix=dir, save_format='jpg'): i += 1 if i > 99: break return image_list # List all the files
def resize(item, target_h, target_w, keep_aspect_ratio=False): """ Resizes an image to match target dimensions :type item: np.ndarray :type target_h: int :type target_w: int :param item: 3d numpy array or PIL.Image :param target_h: height in pixels :param target_w: width in pixels :param keep_aspect_ratio: If False then image is rescaled to smallest dimension and then cropped :return: 3d numpy array """ img = array_to_img(item, scale=False) if keep_aspect_ratio: img.thumbnail((target_w, target_w), PILImage.ANTIALIAS) img_resized = img else: img_resized = img.resize((target_w, target_h), resample=PILImage.NEAREST) # convert output img_resized = img_to_array(img_resized) img_resized = img_resized.astype(dtype=np.uint8) return img_resized
def preprocess_image_crop(image_path, img_size): ''' Preprocess the image scaling it so that its smaller size is img_size. The larger size is then cropped in order to produce a square image. ''' img = load_img(image_path) scale = float(img_size) / min(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) # print('old size: %s,new size: %s' %(str(img.size), str(new_size))) img = img.resize(new_size, resample=Image.BILINEAR) img = img_to_array(img) crop_h = img.shape[0] - img_size crop_v = img.shape[1] - img_size img = img[crop_h:img_size+crop_h, crop_v:img_size+crop_v, :] img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img # util function to open, resize and format pictures into appropriate tensors
def preprocess_image_scale(image_path, img_size=None): ''' Preprocess the image scaling it so that its larger size is max_size. This function preserves aspect ratio. ''' img = load_img(image_path) if img_size: scale = float(img_size) / max(img.size) new_size = (int(np.ceil(scale * img.size[0])), int(np.ceil(scale * img.size[1]))) img = img.resize(new_size, resample=Image.BILINEAR) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img # util function to convert a tensor into a valid image
def test_bare_keras_module(self): """ Keras GraphFunctions should give the same result as standard Keras models """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) for model_gen, preproc_fn in [(InceptionV3, iv3.preprocess_input), (Xception, xcpt.preprocess_input), (ResNet50, rsnt.preprocess_input)]: keras_model = model_gen(weights="imagenet") target_size = tuple(keras_model.input.shape.as_list()[1:-1]) _preproc_img_list = [] for fpath in img_fpaths: img = load_img(fpath, target_size=target_size) # WARNING: must apply expand dimensions first, or ResNet50 preprocessor fails img_arr = np.expand_dims(img_to_array(img), axis=0) _preproc_img_list.append(preproc_fn(img_arr)) imgs_input = np.vstack(_preproc_img_list) preds_ref = keras_model.predict(imgs_input) gfn_bare_keras = GraphFunction.fromKeras(keras_model) with IsolatedSession(using_keras=True) as issn: K.set_learning_phase(0) feeds, fetches = issn.importGraphFunction(gfn_bare_keras) preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_input}) self.assertTrue(np.all(preds_tgt == preds_ref))
def _load_img_pair(self, idx, load_from_memory): """Get a pair of images with index idx.""" if load_from_memory: a = self.a[idx] b = self.b[idx] return a, b fname = self.filenames[idx] a = load_img(os.path.join(self.a_dir, fname), grayscale=self.is_a_grayscale, target_size=self.target_size) b = load_img(os.path.join(self.b_dir, fname), grayscale=self.is_b_grayscale, target_size=self.target_size) a = img_to_array(a, self.dim_ordering) b = img_to_array(b, self.dim_ordering) return a, b
def extract_vgg16_features(x): from keras.preprocessing.image import img_to_array, array_to_img from keras.applications.vgg16 import preprocess_input, VGG16 from keras.models import Model # im_h = x.shape[1] im_h = 224 model = VGG16(include_top=True, weights='imagenet', input_shape=(im_h, im_h, 3)) # if flatten: # add_layer = Flatten() # else: # add_layer = GlobalMaxPool2D() # feature_model = Model(model.input, add_layer(model.output)) feature_model = Model(model.input, model.get_layer('fc1').output) print('extracting features...') x = np.asarray([img_to_array(array_to_img(im, scale=False).resize((im_h,im_h))) for im in x]) x = preprocess_input(x) # data - 127. #data/255.# features = feature_model.predict(x) print('Features shape = ', features.shape) return features
def test(): classes = [] for subdir in sorted(os.listdir('data/train')): if os.path.isdir(os.path.join('data/train', subdir)): classes.append(subdir) m = genmodel() m.load_weights('weights.model') image = load_img('data/predict/c.png', target_size=(48, 48)).convert('L') x = img_to_array(image) x = x.reshape((1,) + x.shape) k = m.predict(x)[0] ks = k.argsort() l = classes print(ks[-1]) print(l[ks[-1]], l[ks[-2]], l[ks[-3]])
def generate_data(): while 1: X_Data = np.empty([0,224,224,3]) Y_Data = np.empty([0]) file_idx = 2200 img_path = '../data/%d.tif' % file_idx csv_path = '../data/%d.csv' % file_idx while os.path.isfile(img_path) and os.path.isfile(csv_path): print img_path + " " + csv_path img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) X_Data = np.append(X_Data,x,axis=0) print img_path # if (file_idx % 20) == 19: yield(X_Data) X_Data = np.empty([0,224,224,3]) file_idx = file_idx + 1 img_path = '../data/%d.tif' % file_idx csv_path = '../data/%d.csv' % file_idx
def predict(model, img, target_size): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (w,h) tuple Returns: list of predicted labels and their probabilities """ if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return preds[0]
def predict(model, img, target_size, top_n=3): """Run model prediction on image Args: model: keras model img: PIL format image target_size: (w,h) tuple top_n: # of top predictions to return Returns: list of predicted labels and their probabilities """ if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return decode_predictions(preds, top=top_n)[0]
def generate_charset(charset, font_file, font_size, invert=False): fill_color, background_color = 'white', 'black' if invert: fill_color, background_color = background_color, fill_color font = ImageFont.truetype(font_file, font_size) max_w, max_h = 0, 0 for char in charset: w, h = font.getsize(char) max_w = max(w, max_w) max_h = max(h, max_h) max_shape = (max_w, max_h) char_imgs = [] for char in charset: img = Image.new('L', max_shape, background_color) draw = ImageDraw.Draw(img) draw.text((0,0), char, font=font, fill=fill_color) char_imgs.append(img_to_array(img)) return np.stack(char_imgs)
def preprocess_input(self, state): if self.atari: # I need to process the state with the previous state # I take the max pixel value of the two frames state = np.maximum(state, self.prev_state) state = np.asarray(state, dtype=np.float32) # convert rgb image to yuv image #yuv = cv2.cvtColor(state,cv2.COLOR_RGB2YUV) yCbCr = Image.fromarray(state, 'YCbCr') # extract the y channel #y, u, v = cv2.split(yuv) y, Cb, Cr = yCbCr.split() # rescale to 84x84 #y_res = cv2.resize(y, (84,84), interpolation=cv2.INTER_AREA) y_res = y.resize((84,84)) y_res = image.img_to_array(y_res) y_res = y_res[np.newaxis,:,:] return y_res elif type(self.S) == gym.spaces.Discrete: # one-hot encoding s = np.zeros(self.S.n) s[state] = 1 return s[np.newaxis,:] # turn vector into matrix else: return state[np.newaxis,:] # turn vector into matrix
def imagefromlist(testlist): sock.sendall('(448, 448, 3)') received = sock.recv(1024) #print received # f = open(testlist) for img_path in f: timg = image.load_img(img_path.strip(), target_size=(448, 448)) xx = image.img_to_array(timg) try: (orgh,orgw,c) = xx.shape #print xx.shape data = xx.reshape(448*448*3).astype(np.uint8) sendimg(sock, data) time.sleep(3) except: continue sock.close()
def predict(imagepath, target_x, target_y, name, model): if imagepath.startswith('http://') or imagepath.startswith('https://') or imagepath.startswith('ftp://'): response = requests.get(imagepath) img = Image.open(BytesIO(response.content)) img = img.resize((target_x, target_y)) else: if not os.path.exists(imagepath): raise Exception('Input image file does not exist') img = image.load_img(imagepath, target_size=(target_x, target_y)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = processInputImage(name, x) preds = decodePrediction(name, model.predict(x)) result = [] for p in preds[0]: result.append({"synset": p[0], "text": p[1], "prediction": float("{0:.2f}".format((p[2] * 100)))}) return json.loads(jsonpickle.encode(result, unpicklable=False))
def load_set(name, lr_sub_size=11, lr_sub_stride=5, scale=3): hr_sub_size = lr_sub_size * scale hr_sub_stride = lr_sub_stride * scale lr_gen_sub = partial(generate_sub_images, size=lr_sub_size, stride=lr_sub_stride) hr_gen_sub = partial(generate_sub_images, size=hr_sub_size, stride=hr_sub_stride) lr_sub_arrays = [] hr_sub_arrays = [] for path in (data_dir / name).glob('*'): lr_image, hr_image = load_image_pair(str(path), scale=scale) lr_sub_arrays += [img_to_array(img) for img in lr_gen_sub(lr_image)] hr_sub_arrays += [img_to_array(img) for img in hr_gen_sub(hr_image)] x = np.stack(lr_sub_arrays) y = np.stack(hr_sub_arrays) return x, y
def is_image_gray(userImage, show_info=True): """ :param image: :return: """ assert userImage img_array = image.img_to_array(userImage) assert len(img_array.shape) == 3 if (len(img_array.shape)) != 3: raise ValueError('The given image is not a valid image...') if (img_array.shape[2]) == 1: utils.helper_functions.show_print_message("Yes, the given image is gray", show_info) return True else: utils.helper_functions.show_print_message("No, the given image is NOT gray", show_info) return False
def main(): # Load the trained model. model = VGG19(weights='imagenet') print_layers(model) # load and preprocess image img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) # predict the class probabilities preds = model.predict(x) # decode the results into a list of tuples (class, description, probability) # (one such list for each sample in the batch) print('--------------------------------') print('Predicted:', decode_predictions(preds, top=3)[0])
def test_pair_crop(crop_function): arr1 = np.random.random(500, 800) arr2 = np.random.random(500, 800) img1 = PILImage.fromarray(arr1) img2 = PILImage.fromarray(arr2) crop_width = img1.width / 5 crop_height = img1.height / 5 result1, result2 = crop_function(img_to_array(img1), img_to_array(img2), (crop_height, crop_width), 'channels_last') result1 = array_to_img(result1) result2 = array_to_img(result2) assert result1.width == crop_width == result2.width assert result2.height == crop_height == result2.height
def create_test_data(self): # ?????npy i = 0 print('-' * 30) print('Creating training images...') print('-' * 30) imgs = glob.glob(self.test_path + "/*." + self.img_type) # deform/train print(len(imgs)) imgdatas = np.ndarray((len(imgs), self.out_rows, self.out_cols, 1), dtype=np.uint8) for imgname in imgs: midname = imgname[imgname.rindex("/") + 1:] # ????? img = load_img(self.test_path + "/" + midname, grayscale=True) # ?????? img = img_to_array(img) imgdatas[i] = img if i % 100 == 0: print('Done: {0}/{1} images'.format(i, len(imgs))) i += 1 print('loading done', imgdatas.shape) np.save(self.npy_path + '/imgs_test.npy', imgdatas) # ?30?????30?label??npy?? # np.save(self.npy_path + '/imgs_mask_train.npy', imglabels) print('Saving to .npy files done.')
def load_dataset(filedir): """ ???? :param filedir: :return: """ image_data_list = [] label = [] train_image_list = os.listdir(filedir + '/train') for img in train_image_list: url = os.path.join(filedir + '/train/' + img) image = load_img(url, target_size=(128, 128)) image_data_list.append(img_to_array(image)) label.append(img.split('-')[0]) img_data = np.array(image_data_list) img_data = img_data.astype('float32') img_data /= 255 return img_data, label
def preprocessImage(imagePath): img = load_img(imagePath, target_size=(244, 244)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) img = img.reshape(img.shape[1:]) return img
def preprocess_image(image_path): img = load_img(image_path, target_size=(im_height, im_width)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img
def load_and_process(img_path, target_size=None): # Feed in the image, convert to array img = load_img(img_path, target_size=target_size) img = img_to_array(img) # Add the batch dimension img = np.expand_dims(img, axis=0) # Perform the usual ImageNet preprocessing img = preprocess_input(img) return img
def get_features(url): response = requests.get(url) img = Image.open(BytesIO(response.content)).convert('RGB') target_size = (224, 224) model = VGG16(weights='imagenet', include_top=False, pooling='avg') if img.size != target_size: img = img.resize(target_size) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) features = model.predict(x).flatten() return features.tolist()
def load_image(img_path): """ Loads an image from a file :param img_path: path to image on disk :return: An instance of PIL.Image target is 'pillow', numpy.ndarray otherwise. """ img = PILImage.open(img_path).convert('RGB') converted_img = img_to_array(img) return converted_img
def loadImage(path): img = image.load_img(path[0], target_size=(299, 299)) x = image.img_to_array(img) x /= 127.5 x -= 1 return x
def preprocess_image(image_path): img = load_img(image_path, target_size=(img_nrows, img_ncols)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img # util function to convert a tensor into a valid image
def preprocess_image(image_path): img = load_img(image_path, target_size=(img_nrows, img_ncols)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg19.preprocess_input(img) return img
def load_mask_labels(): '''Load both target and style masks. A mask image (nr x nc) with m labels/colors will be loaded as a 4D boolean tensor: (1, m, nr, nc) for 'th' or (1, nr, nc, m) for 'tf' ''' target_mask_img = load_img(target_mask_path, target_size=(img_nrows, img_ncols)) target_mask_img = img_to_array(target_mask_img) style_mask_img = load_img(style_mask_path, target_size=(img_nrows, img_ncols)) style_mask_img = img_to_array(style_mask_img) if K.image_dim_ordering() == 'th': mask_vecs = np.vstack([style_mask_img.reshape((3, -1)).T, target_mask_img.reshape((3, -1)).T]) else: mask_vecs = np.vstack([style_mask_img.reshape((-1, 3)), target_mask_img.reshape((-1, 3))]) labels = kmeans(mask_vecs, nb_labels) style_mask_label = labels[:img_nrows * img_ncols].reshape((img_nrows, img_ncols)) target_mask_label = labels[img_nrows * img_ncols:].reshape((img_nrows, img_ncols)) stack_axis = 0 if K.image_dim_ordering() == 'th' else -1 style_mask = np.stack([style_mask_label == r for r in xrange(nb_labels)], axis=stack_axis) target_mask = np.stack([target_mask_label == r for r in xrange(nb_labels)], axis=stack_axis) return (np.expand_dims(style_mask, axis=0), np.expand_dims(target_mask, axis=0)) # Create tensor variables for images
def preprocess_image(image_path): img = load_img(image_path, target_size=(img_width, img_height)) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = vgg16.preprocess_input(img) return img # util function to convert a tensor into a valid image
def get_faces(): X_train = np.concatenate(( [img_to_array(load_img('fine_tune/faces/zero/' + filename, target_size=(224, 224))) for filename in os.listdir('fine_tune/faces/zero/')], [img_to_array(load_img('fine_tune/faces/one/' + filename, target_size=(224, 224))) for filename in os.listdir('fine_tune/faces/one/')])) Y_train = np.concatenate(( np.zeros(len(os.listdir('fine_tune/faces/zero/'))), np.ones(len(os.listdir('fine_tune/faces/one/'))))) assert len(X_train) == len(Y_train) perm = np.random.permutation(len(X_train)) return (X_train[perm], Y_train[perm])
def process_image(image, target_shape): """Given an image, process it and return the array.""" # Load the image. h, w, _ = target_shape image = load_img(image, target_size=(h, w)) # Turn it into numpy, normalize and return. img_arr = img_to_array(image) x = (img_arr / 255.).astype(np.float32) return x
def load_mask_labels(): '''Load both target and style masks. A mask image (nr x nc) with m labels/colors will be loaded as a 4D boolean tensor: (1, m, nr, nc) for 'channels_first' or (1, nr, nc, m) for 'channels_last' ''' target_mask_img = load_img(target_mask_path, target_size=(img_nrows, img_ncols)) target_mask_img = img_to_array(target_mask_img) style_mask_img = load_img(style_mask_path, target_size=(img_nrows, img_ncols)) style_mask_img = img_to_array(style_mask_img) if K.image_data_format() == 'channels_first': mask_vecs = np.vstack([style_mask_img.reshape((3, -1)).T, target_mask_img.reshape((3, -1)).T]) else: mask_vecs = np.vstack([style_mask_img.reshape((-1, 3)), target_mask_img.reshape((-1, 3))]) labels = kmeans(mask_vecs, num_labels) style_mask_label = labels[:img_nrows * img_ncols].reshape((img_nrows, img_ncols)) target_mask_label = labels[img_nrows * img_ncols:].reshape((img_nrows, img_ncols)) stack_axis = 0 if K.image_data_format() == 'channels_first' else -1 style_mask = np.stack([style_mask_label == r for r in xrange(num_labels)], axis=stack_axis) target_mask = np.stack([target_mask_label == r for r in xrange(num_labels)], axis=stack_axis) return (np.expand_dims(style_mask, axis=0), np.expand_dims(target_mask, axis=0)) # Create tensor variables for images
def preprocess_image(image_path): # Util function to open, resize and format pictures # into appropriate tensors. img = load_img(image_path) img = img_to_array(img) img = np.expand_dims(img, axis=0) img = inception_v3.preprocess_input(img) return img
def load_img_arr(p): return img_to_array(load_img(p))
def loadAndPreprocessKerasInceptionV3(raw_uri): # this is the canonical way to load and prep images in keras uri = raw_uri[5:] if raw_uri.startswith("file:/") else raw_uri image = img_to_array(load_img(uri, target_size=InceptionV3Constants.INPUT_SHAPE)) image = np.expand_dims(image, axis=0) return preprocess_input(image)
def _loadImageViaKeras(self, raw_uri): uri = raw_uri[5:] if raw_uri.startswith("file:/") else raw_uri image = img_to_array(load_img(uri)) image = np.expand_dims(image, axis=0) return preprocess_input(image)
def test_spimage_converter_module(self): """ spimage converter module must preserve original image """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) def exec_gfn_spimg_decode(spimg_dict, img_dtype): gfn = gfac.buildSpImageConverter('BGR', img_dtype) with IsolatedSession() as issn: feeds, fetches = issn.importGraphFunction(gfn, prefix="") feed_dict = dict( (tnsr, spimg_dict[tfx.op_name(tnsr, issn.graph)]) for tnsr in feeds) img_out = issn.run(fetches[0], feed_dict=feed_dict) return img_out def check_image_round_trip(img_arr): spimg_dict = imageArrayToStruct(img_arr).asDict() spimg_dict['data'] = bytes(spimg_dict['data']) img_arr_out = exec_gfn_spimg_decode( spimg_dict, imageTypeByOrdinal(spimg_dict['mode']).dtype) self.assertTrue(np.all(img_arr_out == img_arr)) for fp in img_fpaths: img = load_img(fp) img_arr_byte = img_to_array(img).astype(np.uint8) check_image_round_trip(img_arr_byte) img_arr_float = img_to_array(img).astype(np.float32) check_image_round_trip(img_arr_float) img_arr_preproc = iv3.preprocess_input(img_to_array(img)) check_image_round_trip(img_arr_preproc)
def test_pipeline(self): """ Pipeline should provide correct function composition """ img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg')) xcpt_model = Xception(weights="imagenet") stages = [('spimage', gfac.buildSpImageConverter('BGR', 'float32')), ('xception', GraphFunction.fromKeras(xcpt_model))] piped_model = GraphFunction.fromList(stages) for fpath in img_fpaths: target_size = tuple(xcpt_model.input.shape.as_list()[1:-1]) img = load_img(fpath, target_size=target_size) img_arr = np.expand_dims(img_to_array(img), axis=0) img_input = xcpt.preprocess_input(img_arr) preds_ref = xcpt_model.predict(img_input) spimg_input_dict = imageArrayToStruct(img_input).asDict() spimg_input_dict['data'] = bytes(spimg_input_dict['data']) with IsolatedSession() as issn: # Need blank import scope name so that spimg fields match the input names feeds, fetches = issn.importGraphFunction(piped_model, prefix="") feed_dict = dict( (tnsr, spimg_input_dict[tfx.op_name(tnsr, issn.graph)]) for tnsr in feeds) preds_tgt = issn.run(fetches[0], feed_dict=feed_dict) # Uncomment the line below to see the graph # tfx.write_visualization_html(issn.graph, # NamedTemporaryFile(prefix="gdef", suffix=".html").name) self.assertTrue(np.all(preds_tgt == preds_ref))
def convert_file_to_input_node(self, file_path, input_node=None): if input_node is None: input_node = self.get_input_node(0) size = (int(input_node['width']), int(input_node['height'])) if 'http://' in file_path or 'https://' in file_path: local_path = tempfile.mktemp() print("Download input ...") f = open(local_path, 'wb') f.write(urllib.urlopen(file_path).read()) f.close() else: local_path = file_path if input_node['inputType'] == 'list': raise Exception("List input not yet available") else: try: image = Image.open(local_path) except Exception: print(("Could not open %s" % (local_path,))) return [] image = image.resize(size, Image.ANTIALIAS) from keras.preprocessing.image import img_to_array image = self.convert_image_to_node(image, input_node) return image
def convert_image_to_node(self, image, input_node=None): from keras.preprocessing.image import img_to_array if input_node is None: input_node = self.get_input_node(0) if input_node['inputType'] == 'image': image = image.convert("L") image = img_to_array(image) elif input_node['inputType'] == 'image_bgr': image = image.convert("RGB") image = np.asarray(image, dtype='float32') image = image[:, :, ::-1].copy() image = img_to_array(image) else: image = image.convert("RGB") image = img_to_array(image) if 'imageScale' not in input_node: input_node['imageScale'] = 255 if float(input_node['imageScale']) > 0: image = image / float(input_node['imageScale']) return image
def testTFwPrediction(self): keras.backend.set_image_dim_ordering('tf') model = SqueezeNet() img = image.load_img('images/cat.jpeg', target_size=(227, 227)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) decoded_preds = decode_predictions(preds) #print('Predicted:', decoded_preds) self.assertIn(decoded_preds[0][0][1], 'tabby') #self.assertAlmostEqual(decode_predictions(preds)[0][0][2], 0.82134342)