Python keras.applications.resnet50.preprocess_input() Examples

The following are 30 code examples of keras.applications.resnet50.preprocess_input(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module keras.applications.resnet50 , or try the search function .
Example #1
Source File: cluster_resnet.py    From lost with MIT License 12 votes vote down vote up
def main(self):
        self.logger.info('Will load keras model')
        model = ResNet50(weights='imagenet')
        self.logger.info('Keras model loaded')
        feature_list = []
        img_path_list = []
        for raw_file in self.inp.raw_files:
            media_path = raw_file.path
            file_list = os.listdir(media_path)
            total = float(len(file_list))
            for index, img_file in enumerate(file_list):
                img_path = os.path.join(media_path, img_file)
                img_path_list.append(img_path)
                img = image.load_img(img_path, target_size=(224, 224))
                x = keras_image.img_to_array(img)
                x = np.expand_dims(x, axis=0)
                x = preprocess_input(x)
                # extract features
                scores = model.predict(x)
                sim_class = np.argmax(scores)
                print('Scores {}\nSimClass: {}'.format(scores, sim_class))
                self.outp.request_annos(img_path, img_sim_class=sim_class)
                self.logger.info('Requested annotation for: {} (cluster: {})'.format(img_path, sim_class))
                self.update_progress(index*100/total) 
Example #2
Source File: dataset.py    From DeepTL-Lane-Change-Classification with MIT License 6 votes vote down vote up
def load_images_for_keras(self, img_path, target_size=(224, 224)):

        features = []
        filenames = sorted(os.listdir(img_path))

        for filename in filenames:

            img = image.load_img(os.path.join(img_path, filename), target_size=target_size)
            img = image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = preprocess_input(img)

            feature = self.model.predict(img)

            if img is not None:
                features.append(feature)

        return features 
Example #3
Source File: run_bottleneck.py    From CarND-Transfer-Learning-Lab with MIT License 6 votes vote down vote up
def gen(session, data, labels, batch_size):
    def _f():
        start = 0
        end = start + batch_size
        n = data.shape[0]

        while True:
            X_batch = session.run(resize_op, {img_placeholder: data[start:end]})
            X_batch = preprocess_input(X_batch)
            y_batch = labels[start:end]
            start += batch_size
            end += batch_size
            if start >= n:
                start = 0
                end = batch_size

            print(start, end)
            yield (X_batch, y_batch)

    return _f 
Example #4
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 6 votes vote down vote up
def extract_Xception(tensor):
	from keras.applications.xception import Xception, preprocess_input
	return Xception(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #5
Source File: cat_dog.py    From deep_learning with MIT License 6 votes vote down vote up
def pred_data():

    with open('./models/cat_dog.yaml') as yamlfile:
        loaded_model_yaml = yamlfile.read()
    model = model_from_yaml(loaded_model_yaml)
    model.load_weights('./models/cat_dog.h5')

    sgd = Adam(lr=0.0003)
    model.compile(loss='categorical_crossentropy',optimizer=sgd, metrics=['accuracy'])

    images = []
    path='./data/test/'
    for f in os.listdir(path):
        img = image.load_img(path + f, target_size=image_size)
        img_array = image.img_to_array(img)

        x = np.expand_dims(img_array, axis=0)
        x = preprocess_input(x)
        result = model.predict_classes(x,verbose=0)

        print(f,result[0]) 
Example #6
Source File: resnet.py    From Elphas with Apache License 2.0 6 votes vote down vote up
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)

    with graph.as_default():
        preds = model.predict(x)

    return decode_predictions(preds, top=top_n)[0] 
Example #7
Source File: main.py    From Pix2Depth with GNU General Public License v3.0 6 votes vote down vote up
def pix2depth(path, model):
    model_name = 'p2d'
    originalImage = cv2.imread(path)
    loaded_model =  model_list['pix2depth'][model]
    file_name = model+'_'+path.split('/')[-1]
    output_file = os.path.join(output_path,file_name)
    if model =='CNN':
        originalImage = cv2.resize(originalImage,(img_dim,img_dim))
        x = preprocess_input(originalImage/1.)
    elif model == 'CycleGAN':
        test(path)
        os.system('cp gautam/inf_results/imgs/fakeA_0_0.jpg %s' % output_file)
    else:
        originalImage = cv2.resize(originalImage,(256,256))
        x = originalImage/255.
    if not model == 'CycleGAN':
        p1 = get_depth_map(x, loaded_model)
        cv2.imwrite(output_file,p1)
    return output_file 
Example #8
Source File: main.py    From Pix2Depth with GNU General Public License v3.0 6 votes vote down vote up
def depth2pix(path,model):
    model_name = 'd2p'
    originalImage = cv2.imread(path)
    loaded_model =  model_list['depth2pix'][model]
    file_name = model+'_'+path.split('/')[-1]
    output_file = os.path.join(output_path,file_name)
    if model =='CNN':
        img_dim = 256
        originalImage = cv2.resize(originalImage,(img_dim,img_dim))
        x = preprocess_input(originalImage/1.)
    elif model == 'CycleGAN':
        test_dep(path)
        os.system('cp gautam/inf_results/imgs/fakeB_0_0.jpg %s' % output_file)
    else:
        originalImage = cv2.resize(originalImage,(256,256))
        x = originalImage/255.
    if not model == 'CycleGAN':
        p1 = get_depth_map(x, loaded_model)
        cv2.imwrite(output_file,p1)
    return output_file 
Example #9
Source File: ResNet_CAM.py    From ResNetCAM-keras with MIT License 5 votes vote down vote up
def pretrained_path_to_tensor(img_path):
    # loads RGB image as PIL.Image.Image type
    img = image.load_img(img_path, target_size=(224, 224))
    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    x = np.expand_dims(x, axis=0)
    # convert RGB -> BGR, subtract mean ImageNet pixel, and return 4D tensor
    return preprocess_input(x) 
Example #10
Source File: baseline.py    From kinship_prediction with MIT License 5 votes vote down vote up
def read_img(path):
    img = cv2.imread(path)
    return preprocess_input(img) 
Example #11
Source File: categorize.py    From smart_categorizer with Apache License 2.0 5 votes vote down vote up
def get_features(paths):
    features = []
    for batch in tqdm.tqdm(chunks(paths, 16)):
        imgs = [image.img_to_array(image.load_img(f, target_size=(224, 224))) for f in batch]
        imgs = np.array(imgs)
        x = preprocess_input(imgs)
        preds = model.predict(x)
        features.extend(preds[:,0,0,:])
    return np.array(features) 
Example #12
Source File: 05_print_resnet.py    From Practical-Computer-Vision with MIT License 5 votes vote down vote up
def preprocess_img(img):
    # apply opencv preprocessing
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    img = cv2.resize(img,  (224, 224)) 
    img = img[np.newaxis, :, :, :]
    img = np.asarray(img, dtype=np.float)
    
    # further use imagenet specific preprocessing
    # this applies color channel specific mean normalization
    x = preprocess_input(img)
    print(x.shape)
    return x

# read input image and preprocess 
Example #13
Source File: models.py    From ICIAR2018 with MIT License 5 votes vote down vote up
def predict(self, x):
        if self.data_format == "channels_first":
            x = x.transpose(0, 3, 1, 2)
        x = preprocess_resnet(x.astype(K.floatx()))
        return self.model.predict(x, batch_size=self.batch_size) 
Example #14
Source File: model.py    From transfer with MIT License 5 votes vote down vote up
def gen_minibatches(array_dir, array_names, batch_size, architecture, final = False):

    array_names = list(array_names)

    while True:
        # in place shuffle
        np.random.shuffle(array_names)
        array_names_mb = array_names[:batch_size]

        arrays = []
        labels = []
        for array_name in array_names_mb:
            img_path = os.path.join(array_dir, array_name)
            array = np.load(img_path)
            if final:
                if architecture == 'resnet50':
                    array = np.squeeze(resnet_preprocess_input(array[np.newaxis].astype(np.float32)))
                elif architecture == 'xception':
                    array = np.squeeze(xception_preprocess_input(array[np.newaxis].astype(np.float32)))
                else:
                    array = np.squeeze(inception_v3_preprocess_input(array[np.newaxis].astype(np.float32)))

            arrays.append(array)
            labels.append(np.load(img_path.replace('-img-','-label-')))

        yield np.array(arrays), np.array(labels) 
Example #15
Source File: predict_model.py    From transfer with MIT License 5 votes vote down vote up
def multi_predict(aug_gen, models, architecture):
    predicted = []
    for img, _ in aug_gen:
        if architecture == 'resnet50':
            img = resnet_preprocess_input(img[np.newaxis].astype(np.float32))
        elif architecture == 'xception':
            img = xception_preprocess_input(img[np.newaxis].astype(np.float32))
        else:
            img = inception_v3_preprocess_input(img[np.newaxis].astype(np.float32))
        for model in models:
            predicted.append(model.predict(img))
    predicted = np.array(predicted).sum(axis=0)
    pred_list = list(predicted[0])
    return predicted, pred_list 
Example #16
Source File: pre_model.py    From transfer with MIT License 5 votes vote down vote up
def val_pre_model(source_path, folder, img_dim, architechture):

    array_path = os.path.join(source_path, folder)
    pre_model_path = os.path.join(source_path, 'pre_model')
    shutil.rmtree(pre_model_path,ignore_errors=True)
    os.makedirs(pre_model_path)

    if architechture == 'resnet50':
        popped, pre_model = get_resnet_pre_model(img_dim)
    elif architechture == 'xception':
        popped, pre_model = get_xception_pre_model(img_dim)
    else:
        popped, pre_model = get_inception_v3_pre_model(img_dim)

    for (array, label, array_name, label_name) in tqdm(gen_array_from_dir(array_path)):
        if architechture == 'resnet50':
            array = resnet_preprocess_input(array[np.newaxis].astype(np.float32))
        elif architechture == 'xception':
            array = xception_preprocess_input(array[np.newaxis].astype(np.float32))
        else:
            array = inception_v3_preprocess_input(array[np.newaxis].astype(np.float32))
        array_pre_model = np.squeeze(pre_model.predict(array, batch_size=1))

        array_name = array_name.split('.')[0]
        label_name = label_name.split('.')[0]

        img_pre_model_path = os.path.join(pre_model_path, array_name)
        label_pre_model_path = os.path.join(pre_model_path, label_name)

        np.save(img_pre_model_path, array_pre_model)
        np.save(label_pre_model_path, label) 
Example #17
Source File: cnn_architecture.py    From Pix2Depth with GNU General Public License v3.0 5 votes vote down vote up
def read_image(i):
    rPath = rgbPath+'img_%d.jpg' % i
    dPath = depPath+'dep_%d.jpg' % i
    return preprocess_input(cv2.imread(rPath)/1.), np.expand_dims(cv2.imread(dPath, 0),-1)/255. 
Example #18
Source File: named_image_test.py    From spark-deep-learning with Apache License 2.0 5 votes vote down vote up
def test_imagenet_preprocess_input(self):
        # compare our tf implementation to the np implementation in keras
        image = np.zeros((256, 256, 3))

        sess = tf.Session()
        with sess.as_default():
            x = tf.placeholder(tf.float32, shape=[256, 256, 3])
            processed = keras_apps._imagenet_preprocess_input(x, (256, 256)),
            sparkdl_preprocessed_input = sess.run(processed, {x: image})

        keras_preprocessed_input = resnet50.preprocess_input(np.expand_dims(image, axis=0))

        # NOTE: precision errors occur for decimal > 5
        np.testing.assert_array_almost_equal(sparkdl_preprocessed_input, keras_preprocessed_input,
                                             decimal=5) 
Example #19
Source File: dataset.py    From DeepTL-Lane-Change-Classification with MIT License 5 votes vote down vote up
def extract_feature(self, img_path):
        img = image.load_img(img_path, target_size=(331, 331))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)

        self.features = self.model.predict(x) 
Example #20
Source File: features.py    From image-similarity-clustering with MIT License 5 votes vote down vote up
def _extract(fp, model):
        # Load the image, setting the size to 224 x 224
        img = image.load_img(fp, target_size=(224, 224))
        
        # Convert the image to a numpy array, resize it (1, 2, 244, 244), and preprocess it
        img_data = image.img_to_array(img)
        img_data = np.expand_dims(img_data, axis=0)
        img_data = preprocess_input(img_data)

        # Extract the features
        np_features = model.predict(img_data)[0]
        
        # Convert from Numpy to a list of values
        return np.char.mod('%f', np_features) 
Example #21
Source File: test_pieces.py    From spark-deep-learning with Apache License 2.0 5 votes vote down vote up
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) 
Example #22
Source File: test_pieces.py    From spark-deep-learning with Apache License 2.0 5 votes vote down vote up
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, target_size in [(InceptionV3, iv3.preprocess_input, model_sizes['InceptionV3']),
                                      (Xception, xcpt.preprocess_input, model_sizes['Xception']),
                                      (ResNet50, rsnt.preprocess_input, model_sizes['ResNet50'])]:

            keras_model = model_gen(weights="imagenet")
            _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})

            np.testing.assert_array_almost_equal(preds_tgt,
                                                 preds_ref,
                                                 decimal=self.featurizerCompareDigitsExact) 
Example #23
Source File: test_pieces.py    From spark-deep-learning with Apache License 2.0 5 votes vote down vote up
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 = model_sizes['Xception']
            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)

            np.testing.assert_array_almost_equal(preds_tgt,
                                                 preds_ref,
                                                 decimal=self.featurizerCompareDigitsExact) 
Example #24
Source File: CNNFeatures.py    From videofeatures with MIT License 5 votes vote down vote up
def computeFeatures(self, video):
    x = vgg16.preprocess_input(video)
    features = self.model.predict(x)
    return features 
Example #25
Source File: CNNFeatures.py    From videofeatures with MIT License 5 votes vote down vote up
def computeFeatures(self, video):
    x = resnet50.preprocess_input(video)
    features = self.model.predict(x)
    return features.reshape((-1, 2048)) 
Example #26
Source File: nn_feature_extraction.py    From MassImageRetrieval with Apache License 2.0 5 votes vote down vote up
def __fetch_nn_feature(batch_image, batch_file_name):
        batch_image = np.concatenate(batch_image, axis=0)
        x = preprocess_input(batch_image)

        features = res50_model.predict(x)
        features_reduce = features.squeeze()
        for idx in range(len(batch_file_name)):
            image_nn_feature_dict[batch_file_name[idx]] = features_reduce[idx]
        # print(features_reduce)
        print(features_reduce.shape) 
Example #27
Source File: cluster_resnet.py    From lost with MIT License 5 votes vote down vote up
def main(self):
        self.logger.info('Will load keras model')
        model = ResNet50(weights='imagenet')
        self.logger.info('Keras model loaded')
        feature_list = []
        img_path_list = []
        # Request only MIA annotations for annotations of first stage
        # that have been annotated in current iteration cycle.
        img_annos = list(filter(lambda x: x.iteration == self.iteration, 
            self.inp.img_annos))
        total = len(img_annos)
        for index, img_anno in enumerate(img_annos):
            annos = img_anno.to_vec('anno.data')
            if annos:
                types = img_anno.to_vec('anno.dtype')
                img = skimage.io.imread(self.get_abs_path(img_anno.img_path))
                crops, anno_boxes = anno_helper.crop_boxes(annos, types, 
                    img, context=0.01)
                sim_classes = []
                for crop in crops:
                    # img = image.load_img(img_path, target_size=(224, 224))
                    crop_img = image.img_to_array(image.array_to_img(crop, scale=False).resize((224,224)))
                    x = keras_image.img_to_array(crop_img)
                    x = np.expand_dims(x, axis=0)
                    x = preprocess_input(x)
                    # extract features
                    scores = model.predict(x)
                    sim_classes.append(np.argmax(scores))
                self.outp.request_annos(img_anno.img_path, 
                    annos=annos, anno_types=types, anno_sim_classes=sim_classes)
                self.logger.info('Requested annotation for: {}\n{}\n{}'.format(img_anno.img_path, types, sim_classes))
                self.update_progress(index*100/total) 
Example #28
Source File: cluster_kmeans.py    From lost with MIT License 5 votes vote down vote up
def main(self):
        n_cluster = int(self.get_arg('n-clusters'))
        self.logger.info('Will load keras model')
        base_model = ResNet50(weights='imagenet')
        self.logger.info('Keras model loaded')
        layer_code = 'avg_pool'
        # base_model.summary()
        model = Model(inputs=base_model.input, outputs=base_model.get_layer(layer_code).output)
        feature_list = []
        img_path_list = []
        self.logger.info('Will compute CNN features')
        for raw_file in self.inp.raw_files:
            media_path = raw_file.path
            media_path = raw_file.path
            file_list = os.listdir(media_path)
            total = float(len(file_list))
            for index, img_file in enumerate(file_list):
                img_path = os.path.join(media_path, img_file)
                img_path_list.append(img_path)
                img = image.load_img(img_path, target_size=(224, 224))
                x = keras_image.img_to_array(img)
                x = np.expand_dims(x, axis=0)
                x = preprocess_input(x)
                # extract features
                features = model.predict(x)
                feature_list.append(features[0].flatten())
                self.update_progress(index*70/total)
        self.logger.info('Computed CNN feature!')
        self.logger.info('Start KMeans clustering')
        kmeans = KMeans(n_clusters=n_cluster, random_state=0).fit(feature_list)
        self.logger.info('Clustering completed!')
        counter = 0
        for sim_class, img_path in zip(kmeans.labels_, img_path_list):
            self.outp.request_annos(img_path, img_sim_class=sim_class)
            self.logger.info('Requested annotation for: {} (cluster: {})'.format(img_path, sim_class))
            counter += 1
            self.update_progress(70 + (counter*30/len(img_path_list))) 
Example #29
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_VGG16(tensor):
	from keras.applications.vgg16 import VGG16, preprocess_input
	return VGG16(weights='imagenet', include_top=False).predict(preprocess_input(tensor)) 
Example #30
Source File: extract_bottleneck_features.py    From kale with Apache License 2.0 5 votes vote down vote up
def extract_VGG19(tensor):
	from keras.applications.vgg19 import VGG19, preprocess_input
	return VGG19(weights='imagenet', include_top=False).predict(preprocess_input(tensor))