Esempio n. 1
0
def data_generator(data, batch_size):  # 样本生成器,节省内存
    while True:
        batch = np.random.choice(data, batch_size)
        tx, ty = [], []
        for img in batch:
            # pic = Image.open(directory + img)  # .convert('L')
            # pic = misc.imresize(pic, img_size)
            # if pic.shape == (200, 200):
            #     pic = cv2.cvtColor(pic.reshape(200, 200), cv2.COLOR_GRAY2BGR)
            #     pic = misc.imresize(pic, img_size)
            pic = load_img(directory + img, target_size=(200, 200))
            pic = img_to_array(pic)
            pic = pic.reshape((pic.shape[0], pic.shape[1], pic.shape[2]))
            tx.append(pic)
            ty.append(y[img])
        # for i in tx:
        #     print(i.shape)
        tyy = []
        for i in ty:
            yy = np.zeros((230,))
            yy[label_list.index(i)] = 1
            tyy.append(yy)
        ty = np.array(tyy)
        tx = preprocess_input(np.array(tx).astype(float))
        yield tx, ty
    def test_prediction_vs_tensorflow_inceptionV3(self):
        output_col = "prediction"
        image_df = image_utils.getSampleImageDF()

        # An example of how a pre-trained keras model can be used with TFImageTransformer
        with KSessionWrap() as (sess, g):
            with g.as_default():
                K.set_learning_phase(0)    # this is important but it's on the user to call it.
                # nChannels needed for input_tensor in the InceptionV3 call below
                image_string = utils.imageInputPlaceholder(nChannels = 3)
                resized_images = tf.image.resize_images(image_string,
                                                        InceptionV3Constants.INPUT_SHAPE)
                preprocessed = preprocess_input(resized_images)
                model = InceptionV3(input_tensor=preprocessed, weights="imagenet")
                graph = tfx.strip_and_freeze_until([model.output], g, sess, return_graph=True)

        transformer = TFImageTransformer(inputCol="image", outputCol=output_col, graph=graph,
                                         inputTensor=image_string, outputTensor=model.output,
                                         outputMode="vector")
        transformed_df = transformer.transform(image_df.limit(10))
        self.assertDfHasCols(transformed_df, [output_col])
        collected = transformed_df.collect()
        transformer_values, transformer_topK = self.transformOutputToComparables(collected,
                                                                                 "filePath",
                                                                                 output_col)

        tf_values, tf_topK = self._executeTensorflow(graph, image_string.name, model.output.name,
                                                     image_df)
        self.compareClassSets(tf_topK, transformer_topK)
        self.compareClassOrderings(tf_topK, transformer_topK)
        self.compareArrays(tf_values, transformer_values)
Esempio n. 3
0
def predict(image_file):
    img = image.load_img(image_file, target_size=(img_width,img_height))
    x = image.img_to_array(img)
    x = np.expand_dims(x,axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    return preds
    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(img_dtype)
            with IsolatedSession() as issn:
                feeds, fetches = issn.importGraphFunction(gfn, prefix="")
                feed_dict = dict((tnsr, spimg_dict[tfx.op_name(issn.graph, tnsr)]) 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, spimg_dict['mode'])
            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.float)
            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 load_img_and_occuluding(model, path, window_size = (40,40), slide_size = 3, correct_class = 0):
    img = image.load_img(path, target_size=(299,299,3))
    x = image.img_to_array(img)
    x = preprocess_input(x)
    plt.imshow(reverse_preprocessing(x))
    plt.show()
    res = occuluding_img(model, x, window_size, slide_size, correct_class)
    return res 
    def _preprocess_an_image(self, img_path, random_transform=True):
        img = load_img(img_path, target_size=self.IMAGE_SIZE)
        img_array = img_to_array(img)
        if self._image_augmentation_switch and random_transform:
            img_array = self._image_data_generator.random_transform(img_array)
        img_array = inception_v3.preprocess_input(img_array)

        return img_array
Esempio n. 7
0
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 preprocess_image(path, img_size):
    if not os.path.isfile(path):
        return np.zeros((1, img_size, img_size, 3), dtype=np.float32)
    img = Image.open(path).convert('RGB')
    img = img.resize((img_size, img_size))
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x
Esempio n. 9
0
def predict(model, img, target_size):
  """Run model prediction on image
  """
  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 load_fine_tune_googlenet_v3(img):
    # 加载fine-tuning googlenet v3模型,并做预测
    model = InceptionV3(include_top=True, weights='imagenet')
    model.summary()
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    print('Predicted:', decode_predictions(preds))
    plt.subplot(212)
    plt.plot(preds.ravel())
    plt.show()
    return model, x
Esempio n. 11
0
def _buildInceptionV3Session(featurize):
    sess = tf.Session()
    with sess.as_default():
        K.set_learning_phase(0)
        inputImage = imageInputPlaceholder(nChannels=3)
        preprocessed = inception_v3.preprocess_input(inputImage)
        model = InceptionV3(input_tensor=preprocessed, weights="imagenet",
                            include_top=(not featurize))
    return dict(inputTensorName=inputImage.name,
                outputTensorName=model.output.name,
                session=sess,
                inputTensorSize=InceptionV3Constants.INPUT_SHAPE,
                outputMode="vector")
Esempio n. 12
0
    def _preprocessingInceptionV3Transformed(self, outputMode, outputCol):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            resized_images = tf.image.resize_images(image_arr, InceptionV3Constants.INPUT_SHAPE)
            processed_images = preprocess_input(resized_images)
        self.assertEqual(processed_images.shape[1], InceptionV3Constants.INPUT_SHAPE[0])
        self.assertEqual(processed_images.shape[2], InceptionV3Constants.INPUT_SHAPE[1])

        transformer = TFImageTransformer(inputCol="image", outputCol=outputCol, graph=g,
                                         inputTensor=image_arr.name, outputTensor=processed_images,
                                         outputMode=outputMode)
        image_df = image_utils.getSampleImageDF()
        return transformer.transform(image_df.limit(5))
    def _preprocessingInceptionV3Transformed(self, outputMode, outputCol):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            resized_images = tf.image.resize_images(image_arr, InceptionV3Constants.INPUT_SHAPE)
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            processed_images = preprocess_input(imageIO._reverseChannels(resized_images))
        self.assertEqual(processed_images.shape[1], InceptionV3Constants.INPUT_SHAPE[0])
        self.assertEqual(processed_images.shape[2], InceptionV3Constants.INPUT_SHAPE[1])

        transformer = TFImageTransformer(channelOrder='BGR', inputCol="image", outputCol=outputCol, graph=g,
                                         inputTensor=image_arr.name, outputTensor=processed_images,
                                         outputMode=outputMode)
        image_df = image_utils.getSampleImageDF()
        return transformer.transform(image_df.limit(5))
Esempio n. 14
0
def predict(image_file):
    img = image.load_img(image_file, target_size=(299, 299))
    x = image.img_to_array(img)
    x = np.expand_dims(x,axis=0)
    x = preprocess_input(x)

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

    top3 = decode_predictions(preds,top=3)[0]

    predictions = [{'label': label, 'description': description, 'probability': probability * 100.0}
                    for label,description, probability in top3]
    return predictions
    def setUpClass(cls):
        super(NamedImageTransformerImagenetTest, cls).setUpClass()

        # Compute values used by multiple tests.
        imgFiles, images = getSampleImageList()
        imageArray = np.empty((len(images), 299, 299, 3), 'uint8')
        for i, img in enumerate(images):
            assert img is not None and img.mode == "RGB"
            imageArray[i] = np.array(img.resize((299, 299)))

        # Predict the class probabilities for the images in our test library using keras API.
        prepedImaged = inception_v3.preprocess_input(imageArray.astype('float32'))
        model = inception_v3.InceptionV3()
        kerasPredict = model.predict(prepedImaged)
        # These values are used by multiple tests so cache them on class setup.
        cls.imageArray = imageArray
        cls.kerasPredict = kerasPredict
Esempio n. 16
0
def submit_data_generator(data, path_t):
    while True:
        tx, ty = [], []
        for img in data:
            # pic = Image.open(directory + img)  # .convert('L')
            # pic = misc.imresize(pic, img_size)
            # if pic.shape == (200, 200):
            #     pic = cv2.cvtColor(pic.reshape(200, 200), cv2.COLOR_GRAY2BGR)
            #     pic = misc.imresize(pic, img_size)
            pic = load_img(path_t + img, target_size=(200, 200))
            pic = img_to_array(pic)
            pic = pic.reshape((pic.shape[0], pic.shape[1], pic.shape[2]))
            tx.append(pic)
            # print(img)

        tx = preprocess_input(np.array(tx).astype(float))
        yield tx, ty
    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
Esempio n. 18
0
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]
Esempio n. 19
0
    def test_load_image_vs_keras(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(inputCol="image", outputCol=output_col, graph=g,
                                         inputTensor=image_arr, outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col]).astype(np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["filePath"])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            self.assertTrue( (processed == keras_processed).all() )
    def test_load_image_vs_keras_RGB(self):
        g = tf.Graph()
        with g.as_default():
            image_arr = utils.imageInputPlaceholder()
            # keras expects array in RGB order, we get it from image schema in BGR => need to flip
            preprocessed = preprocess_input(image_arr)

        output_col = "transformed_image"
        transformer = TFImageTransformer(channelOrder='RGB', inputCol="image", outputCol=output_col, graph=g,
                                         inputTensor=image_arr, outputTensor=preprocessed.name,
                                         outputMode="vector")

        image_df = image_utils.getSampleImageDF()
        df = transformer.transform(image_df.limit(5))

        for row in df.collect():
            processed = np.array(row[output_col], dtype = np.float32)
            # compare to keras loading
            images = self._loadImageViaKeras(row["image"]['origin'])
            image = images[0]
            image.shape = (1, image.shape[0] * image.shape[1] * image.shape[2])
            keras_processed = image[0]
            np.testing.assert_array_almost_equal(keras_processed, processed, decimal = 6)
Esempio n. 21
0
def read_image(target_size):
    img = image.load_img(img_path, target_size=target_size)
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x
Esempio n. 22
0
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)
Esempio n. 23
0
detections = net.forward()

for i in range(0, detections.shape[2]):
    confidence = detections[0, 0, i, 2]
    # Setting the confidence to greater than 0.4
    if confidence > 0.4:
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
        (startX, startY) = (max(0, startX), max(0, startY))
        (endX, endY) = (min(w - 1, endX), min(h - 1, endY))
        # Preprocessing the live feed frame
        face = img[startY:endY, startX:endX]
        face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB)
        face = cv2.resize(face, (224, 224))
        face = image.img_to_array(face)
        face = preprocess_input(face)
        face = np.expand_dims(face, axis=0)
        # Predicting the frame from the loaded model
        (mask, withoutMask) = model.predict(face)[0]

        label = "Mask" if mask > withoutMask else "No Mask"
        color = (0, 255, 0) if label == "Mask" else (0, 0, 255)

        label = "{}: {:.2f}%".format(label, max(mask, withoutMask) * 100)

        cv2.putText(img, label, (startX, startY - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
        cv2.rectangle(img, (startX, startY), (endX, endY), color, 10)
        cv2.imshow("Frame", img)
        cv2.imwrite('new_image.jpg', img)
        interrupt = cv2.waitKey(10)
 def preprocess(self, inputImage):
     return inception_v3.preprocess_input(inputImage)
Esempio n. 25
0
def predict_genre(filename):
  img = plt.imread(filename)
  text_img = Final_textreader(img)
  img_load = image.load_img(filename, target_size=(299, 299))     
  x = image.img_to_array(img_load)
  x = np.expand_dims(x, axis=0)
  x = preprocess_input(x)
  start = time.time()
  pred_clf_inception = inception_one_image(x) #new_inception prediction
  print("Inception done, {}".format(time.time()-start))
  #pred_clf_inception = pre_pred_clf_inception.argmax(axis=1)
  text_detected = False
  features_img = extract_features_keras(x)
  features_img_pca = pca.transform(features_img)
  pred_clf_svm_inception = clf_SVM_new_inception.predict_proba(features_img_pca) #svm_inception prediction
  print("SVM done, {}".format(time.time()-start))
  print(text_img)
  if text_img  ==  ['']:
  #   print('Text Mining classifier did not find any text on the image')
    total_pred = pd.DataFrame(index=['Top 1', 'Top 2', 'Top 3'],
                        columns=['inception', 'SVM_inception'])
    total_pred_best_pred = pd.DataFrame(index=['Top 1', 'Top 2', 'Top 3'],
                        columns=['inception', 'SVM_inception'])
    total_pred.inception, total_pred_best_pred.inception = classement_predictions(pred_clf_inception)
    total_pred.SVM_inception, total_pred_best_pred.SVM_inception = classement_predictions(pred_clf_svm_inception)

  else :
  #   print('Some text has been found on the image: ',text_img)
    text_detected = " ".join(text_img)
    df_text_img=Corpus_dropna(text_img)
    Filtered_text_img=text_processer(df_text_img.Text,stop_words=stopwords,stemmer=None)
    Text_img_count=countv.transform(Filtered_text_img)
    text_img_to_pred=tformer.transform(Text_img_count)

    pred_clf_textmining = clf_Text.predict_proba(text_img_to_pred)#text prediction
    print("Text done, {}".format(time.time()-start))

    total_pred = pd.DataFrame(index=['Top 1', 'Top 2', 'Top 3'],
                        columns=['Text', 'inception', 'SVM_inception'])

    total_pred_best_pred = pd.DataFrame(index=['Top 1', 'Top 2', 'Top 3'],
                        columns=['Text', 'inception', 'SVM_inception'])

    total_pred.Text, total_pred_best_pred.Text = classement_predictions(pred_clf_textmining)
    total_pred.inception, total_pred_best_pred.inception = classement_predictions(pred_clf_inception)
    total_pred.SVM_inception, total_pred_best_pred.SVM_inception = classement_predictions(pred_clf_svm_inception)
  pred = total_pred_best_pred
  resultats=pd.DataFrame(columns=['Best Predictions'],
                          index=['Top 1', 'Top 2', 'Top 3'])
  if(len(pred.columns) == 3):
    Top1f=choice_matrix[str(pred.Text[0])][pred.SVM_inception[0]]
    if(pred.Text[0]!=pred.SVM_inception[0]):
        if(Top1f!=classe[pred.Text[0]]):
            Top2f=classe[pred.Text[0]]
        else:
            Top2f=classe[pred.SVM_inception[0]]
        Top2Text=classe[pred.Text[1]]
        Top2SVM=classe[pred.SVM_inception[1]]
        if(Top2Text!=Top1f and Top2Text!=Top2f):
            if(Top2SVM!=Top1f and Top2SVM!=Top2f):
                Top3f=choice_matrix[str(pred.Text[1])][pred.SVM_inception[1]]
            else:
                Top3f=Top2Text
        else:
            if(Top2SVM!=Top1f and Top2SVM!=Top2f):
                Top3f=Top2SVM
            else:
                Top3f=choice_matrix[str(pred.Text[2])][pred.SVM_inception[2]]
    else:
        Top2f=choice_matrix[str(pred.Text[1])][pred.SVM_inception[1]]
        if(pred.Text[1]!=pred.SVM_inception[1]):
            if(Top2f!=classe[pred.Text[1]]):
                Top3f=classe[pred.Text[1]]
            else:
                Top3f=classe[pred.SVM_inception[1]]
        else:
            Top3f=choice_matrix[str(pred.Text[2])][pred.SVM_inception[2]]
  else:
      Top1f=choice_matrix_img[str(pred.SVM_inception[0])][pred.inception[0]]
      if(pred.SVM_inception[0]!=pred.inception[0]):
          if(Top1f!=classe[pred.SVM_inception[0]]):
              Top2f=classe[pred.SVM_inception[0]]
          else:
              Top2f=classe[pred.inception[0]]
          Top2Inc=classe[pred.inception[1]]
          Top2SVM=classe[pred.SVM_inception[1]]
          if(Top2SVM!=Top1f and Top2SVM!=Top2f):
              if(Top2Inc!=Top1f and Top2Inc!=Top2f):
                  Top3f=choice_matrix_img[str(pred.SVM_inception[1])][pred.inception[1]]
              else:
                  Top3f=Top2SVM
          else:
              if(Top2Inc!=Top1f and Top2Inc!=Top2f):
                  Top3f=Top2Inc
              else:
                      Top3f=choice_matrix_img[str(pred.SVM_inception[2])][pred.inception[2]]
      else:
          Top2f=choice_matrix[str(pred.SVM_inception[1])][pred.inception[1]]
          if(pred.SVM_inception[1]!=pred.inception[1]):
              if(Top2f!=classe[pred.SVM_inception[1]]):
                  Top3f=classe[pred.SVM_inception[1]]
              else:
                  Top3f=classe[pred.inception[1]]
          else:
              Top3f=choice_matrix_img[str(pred.SVM_inception[2])][pred.inception[2]]
  resultats["Best Predictions"][0]=Top1f
  resultats["Best Predictions"][1]=Top2f
  resultats["Best Predictions"][2]=Top3f
  end = time.time()
  print("Time taken: ",(end-start))
  return resultats.to_html(justify='left', border = 0), text_detected
Esempio n. 26
0
def preprocess_array(arr):
    #version of preprocess_image, except for a numpy img_to_array
    img = np.expand_dims(arr, axis=0)
    img = inception_v3.preprocess_input(img)
    return img
Esempio n. 27
0
def load_image(image_path):
    img = image.load_img(image_path, target_size=(128, 128))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x
def process(x):
    return preprocess_input(x)
Esempio n. 29
0
from keras.applications import inception_v3
from keras.preprocessing import image
import numpy as np

model = inception_v3.InceptionV3()

img = image.load_img("img/sigiriya/300px-Sigiriya.jpg", target_size=(299,299))

x = image.img_to_array(img)

x = np.expand_dims(x, axis=0)

x = inception_v3.preprocess_input(x)

pred = model.predict(x)

print(model.summary())

classes = inception_v3.decode_predictions(pred, top=5)

print(classes)

Esempio n. 30
0
from keras.models import Model
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.optimizers import adam, rmsprop, sgd
from keras.preprocessing.image import ImageDataGenerator
from keras.applications.inception_v3 import preprocess_input
from keras.datasets import cifar10
from keras.utils.np_utils import to_categorical

train = 'train/'
test = 'test/'

config = tf.ConfigProto()
config.gpu_options.allow_growth = True

cifar_data = cifar10.load_data()
train_data = preprocess_input((cifar_data[0][0]).astype('float32'))
train_label = cifar_data[0][1]
test_data = preprocess_input((cifar_data[1][0]).astype('float32'))
test_label = cifar_data[1][1]
train_label = to_categorical(train_label, 10)
test_label = to_categorical(test_label, 10)


# Seting input size to 256x256 or else downstream features get too tiny for smaller images
def Resize(x):
    y = tf.image.resize_bicubic(x, size=(256, 256))
    return y


'''
def creat_model():
Esempio n. 31
0
    class LucidInceptionV3(Model):
        # model_path = 'keras_inception_v3_frozen.pb.modelzoo'
        model_path = args.modelzoo_file
        image_shape = [299, 299, 3]
        image_value_range = (0, 1)
        input_name = 'input_1'

    keras_model = InceptionV3(weights='imagenet', input_shape=(299, 299, 3))
    inceptionv3 = LucidInceptionV3()
    inceptionv3.load_graphdef()

    # Image Processing
    img_path = args.input_image_path
    img = image.load_img(img_path, target_size=(299, 299))
    x = image.img_to_array(img)
    x = preprocess_input(x)
    y = image.img_to_array(img)
    y = np.expand_dims(y, axis=0)
    y = preprocess_input(y)

    layer_idx = args.conv_layer_idx
    for i in range(len(keras_model.layers)):
        if keras_model.layers[i].name == "conv2d_{}".format(layer_idx):
            keras_layer_idx = i
            break
    layer_name = "conv2d_{}/convolution".format(layer_idx)
    filter_idx = args.filter_idx
    n_of_filter = args.num_of_filters

    if filter_idx is None:
        filters = [
Esempio n. 32
0
from keras.models import load_model

model_mbn2 = load_model("models/t6_0.7002_1.0559_93_mbn2.model")
model_iv3_1 = load_model("models/t6_0.7396_0.9810_84_iv3.model")
model_iv3_2 = load_model("models/t6_0.7287_0.9836_59_iv3.model")

model_mbn2.name = 'model_mbn2'
model_iv3_1.name = 'model_iv3_1'
model_iv3_2.name = 'model_iv3_2'

models = [model_mbn2, model_iv3_1, model_iv3_2]
ensemble_model = ensemble(models, model_input)
ensemble_model.summary()

# In[ ]:
target_size = (299, 299)
yTest = np.zeros((len(xTest)))
for im in range(0, len(xTest)):
    imR1 = cv2.resize(xTest[im],
                      dsize=target_size,
                      interpolation=cv2.INTER_NEAREST)
    imR = np.expand_dims(imR1, axis=0)
    #imR = np.expand_dims(xTest[im], axis=0)
    imR = preprocess_input(imR.astype(np.float32))
    yTest[im] = np.argmax(ensemble_model.predict(imR))
    print("Finished image --- " + str(im) + "==" + str(yTest[im]))

df = pd.DataFrame(yTest)
df.to_csv("Task8/submission_t6_en.csv")
    fid = ssdiff + trace(sigma1 + sigma2 - 2.0 * covmean)
    return fid


# prepare the inception v3 model
model = InceptionV3(include_top=False,
                    pooling='avg',
                    input_shape=(299, 299, 3))
# define two fake collections of images
images1 = randint(0, 255, 1 * 32 * 32 * 3)
images1 = images1.reshape((1, 32, 32, 3))
images2 = randint(0, 255, 1 * 32 * 32 * 3)
images2 = images2.reshape((1, 32, 32, 3))
print('Prepared', images1.shape, images2.shape)
# convert integer to floating point values
images1 = images1.astype(np.uint8)
images2 = images2.astype(np.uint8)
# resize images
images1 = scale_images(images1, (299, 299, 3))
images2 = scale_images(images2, (299, 299, 3))
print('Scaled', images1.shape, images2.shape)
# pre-process images
images1 = preprocess_input(images1)
images2 = preprocess_input(images2)
# fid between images1 and images1
fid = calculate_fid(model, images1, images1)
print('FID (same): %.3f' % fid)
# fid between images1 and images2
fid = calculate_fid(model, images1, images2)
print('FID (different): %.3f' % fid)
Esempio n. 34
0
    # load our original input image
    orig = cv2.imread(p)
    # pre-process our image by converting it from BGR to RGB channel
    # ordering (since our Keras mdoel was trained on RGB ordering),
    # resize it to 64x64 pixels, and then scale the pixel intensities
    # to the range [0, 1]

    # order channel dimensions (channels-first or channels-last)
    # depending on our Keras backend, then add a batch dimension to
    # the image
    image = load_img(p, target_size=(150, 150))
    image = img_to_array(image)  #output Numpy-array

    image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))

    image = preprocess_input(image)
    x = model.predict(image)[0]

    g = ("{:.1f}%".format(x[0] * 100))

    label = "snake" if x[0] > .85 else "no snake"
    color = (0, 0, 255) if x[0] < .85 else (0, 255, 0)

    # resize our original input (so we can better visualize it) and
    # then draw the label on the image
    orig = cv2.resize(orig, (128, 128))
    cv2.putText(orig, label, (3, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
    cv2.putText(orig, g, (3, 35), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
    # add the output image to our list of results
    results.append(orig)
 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)
Esempio n. 36
0
model = Model(inputs=base_model.input, outputs=predictions)

model.load_weights("Inceptionv3_2.h5")

#Performance Assesment :
import os

#For Clean Cars :
root_path = "../PICS/testnew/"
direc = "Clean/"
pred_sum = 0
for pic in os.listdir(root_path + direc):
    img = image.load_img(root_path + direc + pic, target_size=(250, 400))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    preds = int(np.round(model.predict(img)))
    pred_sum += preds
print("False Positive Rate :")
print(pred_sum / len(os.listdir(root_path + direc)))

#For Damaged Cars :
root_path = "../PICS/testnew/"
direc = "Damaged/"
pred_sum = 0
idx = 1
for pic in os.listdir(root_path + direc):
    img = image.load_img(root_path + direc + pic, target_size=(250, 400))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
Esempio n. 37
0
def video_summarizer(original_videos_folder,
                     trained_model="models_new/2_73.822.pth.tar"):
    hps = HParameters()
    ao = AONet(hps)
    ao.initialize()
    ao.load_model(trained_model)

    model = InceptionV3(include_top=False,
                        weights='imagenet',
                        input_tensor=None,
                        input_shape=None,
                        pooling='avg',
                        classes=1000)

    f = os.listdir(original_videos_folder)
    f.sort()
    f = ["videos/" + s for s in f if s[-1] == "4"]

    #user_sumery_list=get_user_sumery_list()
    #f=["20190425_165804.mp4"]

    for ind_vid, vidio_path in enumerate(f):
        cap = cv2.VideoCapture(vidio_path)
        fps = int(cap.get(cv2.CAP_PROP_FPS))
        length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
        frame = None
        frames = []

        for index in range(length):
            ret, frame = cap.read()

            try:
                frame = cv2.resize(frame, (299, 299))
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                frames.append(frame)
            except:
                print(index)
                print(vidio_path)
                frames.append(frames[-1])
        frames = np.array(frames)
        frames = inception_v3.preprocess_input(frames)
        predictions = model.predict(frames)

        pred_sub_sump = predictions[np.arange(0, len(predictions), 15)]

        seq = pred_sub_sump
        seq = torch.from_numpy(seq).unsqueeze(0)

        seq = seq.float().cuda()

        #predict

        y, att_vec = ao.model(seq, seq.shape[1])

        cps = change_points(predictions, length, fps, vidio_path)
        num_frames = length
        positions = np.array(range(0, num_frames, 15), dtype=np.int64)
        probs = y[0].detach().cpu().numpy()
        #user_sumery=user_sumery_list[ind_vid]
        # plt.title(vidio_path[7:-4])
        # plt.bar(list(range(len(probs))), probs/np.sum(probs),alpha=0.4,color="r", linewidth=0.5)
        # plt.bar(list(range(len(user_sumery))), user_sumery/np.sum(user_sumery),alpha=0.4,color="b", linewidth=0.5)
        # plt.savefig(vidio_path[7:-4]+"plt"+"png")
        # #plt.show()

        nfps = [j - i + 1 for i, j in cps]

        machine_summary = generate_summary(probs, cps, num_frames, nfps,
                                           positions)

        fourcc = cv2.VideoWriter_fourcc(*'XVID')
        cap = cv2.VideoCapture(vidio_path)
        out = cv2.VideoWriter(vidio_path[:-4] + ".avi", fourcc, fps,
                              (int(cap.get(3)), int(cap.get(4))))
        for index in range(length):
            ret, frame = cap.read()
            if machine_summary[index] > 0.5:
                out.write(frame)
# In[7]:


#from keras.applications.inception_v3 import InceptionV3, preprocess_input
from matplotlib.pyplot import imshow

import numpy as np
import glob
folder = './data/images/test/'
for gender in ['male','female']:
    for img_path in glob.glob(folder + gender +'/*.jpg')[:30]:
        #img_path = os.path.join('./data/images/test/', '/pic_153.jpg')
        img = image.load_img(img_path, target_size=(299,299,3))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        #print('Input image shape:', x.shape)

        preds = model.predict(x)
        pred_class = 'female' if preds.argmax()==0 else 'male'
        if gender != pred_class:
            print('Actual Class: %s , Pred class: %s , Scores: %s' % (gender,pred_class, str(preds)))
            plt.imshow(img)
            plt.show()


# In[8]:


def reverse_preprocessing(x):
    x_reversed = x/2.
from keras.datasets import cifar10
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from scipy.misc import imresize
from keras.utils import to_categorical
import numpy as np
#Data preprocessing

(X_train, Y_train), (X_test, Y_test) = cifar10.load_data()
X_train = np.array([imresize(x, (139, 139, 3))
                    for x in X_train[:10]]).astype('float32')
X_test = np.array([imresize(x, (139, 139, 3))
                   for x in X_test[:10]]).astype('float32')
X_train = preprocess_input(X_train)
X_test = preprocess_input(X_test)
Y_train = to_categorical(Y_train, num_classes=10)
Y_test = to_categorical(Y_test, num_classes=10)

#Importing Inception and extracting features

model = InceptionV3(include_top=False, input_shape=(139, 139, 3))
features_train = model.predict(X_train)
features_test = model.predict(X_test)

#saving features_to files
np.save('trainingfeatures', features_train)
np.save('testingfeatures', features_test)
np.save('traininglabels', Y_train)
np.save('testinglabels', Y_test)
X_val, y_val = np.load('../../data/train/X_val.npy'), np.load('../../data/train/y_val.npy')
y_tr = y_tr.astype(np.float32)
y_val = y_val.astype(np.float32)
print 'X_tr.shape:', X_tr.shape
print 'y_tr.shape:', y_tr.shape
print 'X_val.shape:', X_val.shape
print 'y_val.shape:', y_val.shape
# # Merge
# X = np.concatenate([X_tr, X_val], axis=0)
# y = np.array(list(y_tr) + list(y_val))
# y = y.astype(np.float32)
# print 'X.shape:', X.shape
# print 'y.shape:', y.shape

#
X_val = inception_v3.preprocess_input(X_val.astype(np.float16))
print 'Preprocess done. (Only Validation)'


# Preprocess func
def pre_func(im):
    im = im.astype(np.float16)
    im = im / 255.0
    im = im - 0.5
    im = im * 2.0
    return im

##################################

# Constants
INPUT_SHAPE = (256, 256, 3)
Esempio n. 41
0
         model.set_weights(ws)
         wp = np.copy(ws)
         for index2, w in enumerate(ws):
             shape = w.shape
             if len(shape) == 4:
                 noise = np.random.normal(0, sigma,
                                          (w.shape[0], w.shape[1],
                                           w.shape[2], w.shape[3]))
                 wp[index2] = ws[index2] + noise
         model.set_weights(wp)
         for i in range(0, n_selected_batch):
             images_path = os.path.join(
                 input_folder, 'x_val_' + str(i).zfill(3) + '.npy')
             array = np.load(images_path)
             X[i * samples_per_batch:(i + 1) *
               samples_per_batch] = preprocess_input(array)
         y_val_pred_p = model.predict(X, batch_size=64, verbose=1)
         y_val_pred_ensemble[seed_index] = y_val_pred_p
         nll_val = log_loss(y_val, y_val_pred_p)
         print('sigma: {0:.6f}, seed: {1}, nll: val {2:.4f}'.format(
             sigma, seed, nll_val))
     y_val_pred_ensemble_mean = np.mean(y_val_pred_ensemble, axis=0)
     nll = log_loss(y_val, y_val_pred_ensemble_mean)
     nlls[sigma] = nll
     nlls_log.append(
         OrderedDict({
             "iteration": iteration,
             "sigma": sigma,
             "nll": nll
         }))
 else:
 def preprocess(self, inputImage):
     # Keras expects RGB order
     return inception_v3.preprocess_input(_reverseChannels(inputImage))
            return
        print "%s: %s" % (key, value)
        i += 1

elephant_pic = 'elephant.jpg'
peacock_pic = 'peacock.jpg'

model = InceptionV3(weights='imagenet', include_top=True)

elephant_img = IMG.open(elephant_pic)
peacock_img = IMG.open(peacock_pic)
elephant = image.img_to_array(elephant_img)
peacock = image.img_to_array(peacock_img)
elephant = np.expand_dims(elephant, axis=0)
peacock = np.expand_dims(peacock, axis=0)
elephant = preprocess_input(elephant)
peacock = preprocess_input(peacock)

elephant_preds = model.predict(elephant)
peacock_preds = model.predict(peacock)

print("KERAS")
print('Elephant Probabilities:\n', decode_predictions(elephant_preds, top=3))
print('Peacock Probabilities:\n', decode_predictions(peacock_preds, top=3))

scale = 2./255

coreml_model = coremltools.converters.keras.convert(model,
                                                    input_names=['image'],
                                                    output_names=['probabilities'],
                                                    image_input_names='image',
image_batch = np.expand_dims(numpy_image, axis=0)
print('image batch size', image_batch.shape)

#vgg
processed_image = vgg16.preprocess_input(image_batch.copy())
predictions = vgg_model.predict(processed_image)
label = decode_predictions(predictions)
print("vgg", label)

#resnet
processed_image = resnet50.preprocess_input(image_batch.copy())
predictions = resnet_model.predict(processed_image)
label_resnet = decode_predictions(predictions, top=3)
print("resnet", label_resnet)

#mobileNet
processed_image = mobilenet.preprocess_input(image_batch.copy())
predictions = mobilenet_model.predict(processed_image)
label_mobilenet = decode_predictions(predictions)
print("mobilenet", label_mobilenet)

#inception_v3
#Has different input size
original = load_img(filename, target_size=(299, 299))
numpy_image = img_to_array(original)
image_batch = np.expand_dims(numpy_image, axis=0)
processed_image = inception_v3.preprocess_input(image_batch.copy())
predictions = inception_model.predict(processed_image)

label_inception = decode_predictions(predictions)
print("inception", label_inception)
Esempio n. 45
0
def loadAndPreprocessKerasInceptionV3_save(uri):
    # this is a typical way to load and prep images in keras
    image = img_to_array(load_img(uri, target_size=(299, 299)))
    image = np.expand_dims(image, axis=0)
    return preprocess_input(image)
Esempio n. 46
0
 def predict(model, img):
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = preprocess_input(x)
     preds = model.predict(x)
     return preds[0]
Esempio n. 47
0
                'posts': jsonfile['numberPosts'],
                'followers': jsonfile['numberFollowers'],
                'following': jsonfile['numberFollowing'],
                'images': []
            }

            # extract and compress images from the user's instagram page
            for image in jsonfile['posts']:
                if not image['isVideo']:
                    try:
                        # open image from URL and obtain compressed array representation for keras models
                        res = urllib.request.urlopen(image['urlImage'])

                        img = img_to_array(load_img(res, target_size=in_shape))
                        img = np.expand_dims(img_to_array(img), axis=0)
                        processed_img = preprocess_input(img)

                        # save images in image directory
                        savename = 'images/' + user_data[
                            'user'] + '_' + ''.join(
                                random.choices(
                                    string.ascii_uppercase + string.digits,
                                    k=15)) + '.jpg'
                        io.imsave(
                            savename,
                            np.reshape(img, (in_shape[0], in_shape[1], 3)))

                        # conduct image classification with Inception ResNet model
                        predictions = model.predict(processed_img)
                        predictions = imagenet_utils.decode_predictions(
                            predictions)
Esempio n. 48
0
                images_aug_tr, keypoints_aug_tr = SeqAug(images=X_train,
                                                         keypoints=y_train)
                tar_train_reg, tar_train_cat = key2Target(
                    keypoints_aug_tr, name)
                images_aug_te, keypoints_aug_te = SeqAugTest(images=X_test,
                                                             keypoints=y_test)
                tar_test_reg, tar_test_cat = key2Target(keypoints_aug_te, name)

                for ind, im in enumerate(images_aug_tr):
                    im = im[1000:1500, :, :]  # Crop out only test area
                    #im = gaussBlur(im)
                    #im = gaborFilt(im)
                    #im = normalize(LOG(im,LOGker))
                    #im = enhanceImage(im)
                    if (useTransferLearning):
                        xx_tr.append(preprocess_input(im))
                    else:
                        #im = im/255.0
                        #im = np.array(im,dtype=np.float32)
                        yuv_im = im  #cv2.cvtColor(im, cv2.COLOR_RGB2YCrCb)
                        xx_tr.append(yuv_im)
                for ii in tar_train_reg:
                    yy_reg_tr.append(ii)
                for ind, ii in enumerate(tar_train_cat):
                    if False:  # Set to true to save train images augmentated
                        images_aug_tr[ind] = cv2.putText(
                            images_aug_tr[ind], str(tar_train_cat[ind]),
                            (0, 20), font, 0.5, (255, 0, 0), 2, cv2.LINE_AA)
                        images_aug_tr[ind] = cv2.circle(
                            images_aug_tr[ind],
                            (50, int(keypoints_aug_tr[ind][0][1])), 5,
Esempio n. 49
0
def preprocess_image(image_path):
    img = image.load_img(image_path)
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = inception_v3.preprocess_input(img)
    return img
Esempio n. 50
0
 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)
    cv_img = cv2.imread(img)
    img_resize = cv2.resize(cv_img, (299, 299))
    images.append(img_resize)

x = pd.DataFrame.from_dict(new_dict, orient='index').reset_index()
x.rename(columns={'index': 'image'}, inplace=True)
"""
Creating instance of a pretrained Inception V3 Convolutional Neural Network
to retrieve image features.
"""
model = InceptionV3(weights='imagenet', include_top=True)
model_new = Model(model.input, model.layers[-2].output)

image_dict = {}
for i in range(len(images)):
    x = preprocess_input(images[i])
    x = np.resize(x, (1, 299, 299, 3))
    preds = model_new.predict(x)
    preds = np.reshape(preds, preds.shape[1])
    image_dict[image_names[i]] = preds
"""
Creating seperate dataframes for train, test and validation images
to create pickle files
"""
train_images = {}
test_images = {}
dev_images = {}

for i in range(len(train_df)):
    if train_df.iloc[i][0] in image_dict.keys():
        train_images[train_df.iloc[i][0]] = image_dict.get(train_df.iloc[i][0])
def pre_process(video_dir):
    classes = []
    X = []
    y = []
    all_videos_path = video_dir


    if not os.path.exists('images'):
        os.makedirs('images')

    for fld in os.listdir(all_videos_path):
        classes.append(fld)

    for fld in os.listdir(all_videos_path):
        for file in os.listdir(all_videos_path+fld):
            video_path = str(all_videos_path+fld+'/'+file)
            video_length = float(str(getLength(video_path)).split(",")[0].split(":")[-1])

            if video_length < 1:
                print("[WARN] Skipping video because of wrong length")
                pass
            else:
                imagesFolder = "images"
                cap = cv2.VideoCapture(video_path)
                frameRate = cap.get(5)
                count = 0

                if video_length < 1.5:
                    controller = 4
                elif video_length < 2.5:
                    controller = 6
                elif video_length < 3.5:
                    controller = 8
                elif video_length < 5.5:
                    controller = 10
                elif video_length < 8.5:
                    controller = 12
                else:
                    controller = 12

                folder = '/tf/CNN_LSTM/images'
                for the_file in os.listdir(folder):
                    file_path = os.path.join(folder, the_file)
                    try:
                        if os.path.isfile(file_path):
                            os.unlink(file_path)
                    except Exception as e:
                        print(e)


                while(cap.isOpened()):
                    frameId = cap.get(1) 
                    ret, frame = cap.read()
                    if (ret != True):
                        break
                    if (frameId%controller==0):
                        count = count + 1
                        filename = imagesFolder + "/image_" +  str(int(count)) + ".jpg"
                        cv2.imwrite(filename, frame)

                    if frameId == controller*6:
                        break
                cap.release()
                print ("[INFO] Video Converted")

                files = []
                for f in os.listdir('images'):
                    if f.endswith(".jpg"):
                        files.append("images/"+str(f))

                sequence = []

                for f in files:
                    img = image.load_img(f, target_size=(299, 299))
                    x = image.img_to_array(img)
                    x = np.expand_dims(x, axis=0)
                    x = preprocess_input(x)
                    features = extactor_model.predict(x)
                    sequence.append(features)

                label_encoded = classes.index(str(fld))
                label_hot = to_categorical(label_encoded, len(classes))


                X.append(sequence)
                y.append(label_hot)
                print("[INFO] Features Extracted for {} Frames at controller {} for class {}".format(len(sequence),controller,fld))

    np.save("X_data",X)            
    np.save("y_data",y)
Esempio n. 53
0
# 画像識別(inceptionV3)

from keras.preprocessing import image
from keras.applications.inception_v3 import preprocess_input, decode_predictions, InceptionV3
import numpy as np

# モデル読み込み(InceptionV3)
model = InceptionV3(weights='imagenet')

# 画像読み込み
img_path = 'test.jpg'
img = image.load_img(img_path, target_size=(299, 299))

# 前処理
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# 予測
preds = model.predict(x)

# 表示
for p in decode_predictions(preds, top=5)[0]:
    print("Score {}, Label {}".format(p[2], p[1]))
Esempio n. 54
0
 def keras_load_and_preproc(fpath):
     img = load_img(fpath, target_size=(299, 299))
     img_arr = img_to_array(img)
     img_iv3_input = iv3.preprocess_input(img_arr)
     return np.expand_dims(img_iv3_input, axis=0)
Esempio n. 55
0
 def preprocess_input(x):
     img = inception_v3.preprocess_input(image.img_to_array(x))
     return image.array_to_img(img)
Esempio n. 56
0
import os
import numpy as np
from keras.models import load_model
from keras.preprocessing.image import img_to_array, load_img
from keras.applications.inception_v3 import preprocess_input

model = load_model("hybrid.h5")
svmFit = load('hybrid_svmFit.joblib')
data = []

folderPath = "/Users/lvz/PycharmProjects/pothole_classifier/test"
for imageName in os.listdir(folderPath):
    if imageName != ".DS_Store":
        print("[INFO] classifying {}".format(imageName))
        image = load_img(folderPath + "/" + imageName, target_size=(299, 299))
        img_data = img_to_array(image)
        img_data = np.expand_dims(img_data, axis=0)

        # extract a color histogram feature from the image
        img_data = preprocess_input(img_data)
        feature = model.predict(img_data)
        feature_np = np.array(feature)
        data.append(feature_np.flatten())

    else:
        continue

labels = svmFit.predict(data)
print(labels)
def save_bottlebeck_features(model_name):

    if model_name == 'resnet50':
        model = resnet50.ResNet50(weights='imagenet',
                                  include_top=False,
                                  pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/2)resnet50/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/2)resnet50/'
        ]

        # 2048 dimensional features
        # pooling: 1) None: output is 16x16x2048, 2) avg: 1x1x2048, 3) max: 1x1x2048
        #base_model=resnet50.ResNet50(input_shape=(img_height,img_width,3),weights='imagenet', include_top=False)
        #model = Model(inputs=base_model.input, outputs=base_model.get_layer('activation_25').output)
    elif model_name == 'nasnet_large':
        model = nasnet.NASNetLarge(input_shape=(img_height, img_width, 3),
                                   weights='imagenet',
                                   include_top=False,
                                   pooling='avg')
        #4032 dimensional features
    elif model_name == 'xception':
        model = xception.Xception(input_shape=(img_height, img_width, 3),
                                  weights='imagenet',
                                  include_top=False,
                                  pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/4)xception/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/4)xception/'
        ]

        #2048 dimensional features
    elif model_name == 'inceptionv3':
        model = inception_v3.InceptionV3(input_shape=(img_height, img_width,
                                                      3),
                                         weights='imagenet',
                                         include_top=False,
                                         pooling='avg')
        feat_output = [
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/5)inceptionv3/',
            'E:/Hongming/projects/tcga-bladder-mutationburden/feature_output/P_CN_20X/5)inceptionv3/'
        ]
        #2048 dimensional features
    elif model_name == 'inceptionresnetv2':
        model = inception_resnet_v2.InceptionResNetV2(input_shape=(img_height,
                                                                   img_width,
                                                                   3),
                                                      weights='imagenet',
                                                      include_top=False,
                                                      pooling='avg')
        #1536 dimensional features
    elif model_name == 'densenet':
        model = densenet.DenseNet201(input_shape=(img_height, img_width, 3),
                                     weights='imagenet',
                                     include_top=False,
                                     pooling='avg')
        # 1920 dimensional features
    else:
        model = vgg19.VGG19(weights='imagenet',
                            include_top=False,
                            pooling='avg')
        # 512 dimensional features
        #base_model=vgg19.VGG19(input_shape=(img_height,img_width,3),weights='imagenet', include_top=False)
        #model=Model(inputs=base_model.input,outputs=base_model.get_layer('block4_pool').output)

    #for i,layer in enumerate(model.layers):
    #    print(i,layer.name)

    #print(model.summary())

    for ind in range(1, len(img_path)):
        path_ind = img_path[ind]
        #path_split=path_ind.split("/")
        images = os.listdir(path_ind)
        for image_name in images:
            if '.svs' in image_name:
                patient_id = image_name[0:23]
                image_features = []
                image_names = []

                ss = time.time()
                patient_id = 'TCGA-2F-A9KO'
                #patches=os.listdir(train_data_dir[ind]+patient_id+'*.png')
                patches = glob.glob(train_data_dir[0] + patient_id + '*.png')

                for patch_name in patches:
                    patch_split = patch_name.split("\\")
                    img = image.load_img(patch_name,
                                         target_size=(img_height, img_width))
                    # convert image to numpy array
                    x = image.img_to_array(img)

                    # the image is now in an array of shape (224, 224, 3)
                    # need to expand it to (1, 224, 224, 3) as it's expecting a list
                    x = np.expand_dims(x, axis=0)
                    #imshow(np.uint8(x[0,:,:,:]))

                    if model_name == 'resnet50':
                        x = resnet50.preprocess_input(x)
                    elif model_name == 'nasnet_large':
                        x = nasnet.preprocess_input(x)
                    elif model_name == 'xception':
                        x = xception.preprocess_input(x)
                    elif model_name == 'inceptionv3':
                        x = inception_v3.preprocess_input(x)
                    elif model_name == 'inceptionresnetv2':
                        x = inception_resnet_v2.preprocess_input(x)
                    elif model_name == 'densenet':
                        x = densenet.preprocess_input(x)
                    else:
                        x = vgg19.preprocess_input(x)

                    # extract the features
                    features = model.predict(x)[0]
                    #features=np.mean(features,axis=(0,1))

                    image_features.append(features)
                    image_names.append(patch_split[1])

                se = time.time() - ss
                print(se)
                if save_features == True:
                    scipy.io.savemat(feat_output[ind] + patient_id +
                                     '_feat.mat',
                                     mdict={
                                         'image_features': image_features,
                                         'image_names': image_names
                                     })