示例#1
0
def get_training_data_mobilenet():
    filenames = os.listdir(TRAIN_PATH)
    shuffle(filenames)
    X_train = []
    y_train = []
    total_hate = 0
    total_no_hate = 0
    print '*****TRAIN*****'
    for filename in filenames:
        if "no_hate" in filename:
            y_train.append([1, 0])
            total_no_hate += 1
        else:
            y_train.append([0, 1])
            total_hate += 1
        img = image.load_img(TRAIN_PATH + filename, target_size=(224, 224))
        x = image.img_to_array(img)
        x = mobilenet.preprocess_input(x)
        X_train.append(x)
    print 'Total train examples: ' + str(len(y_train))
    print 'No hate examples: ' + str(total_no_hate)
    print 'Hate examples: ' + str(total_hate)

    print '*****VAL*****'
    filenames = os.listdir(VAL_PATH)
    shuffle(filenames)
    X_val = []
    y_val = []
    total_hate = 0
    total_no_hate = 0
    for filename in filenames:
        if "no_hate" in filename:
            y_val.append([1, 0])
            total_no_hate += 1
        else:
            y_val.append([0, 1])
            total_hate += 1
        img = image.load_img(VAL_PATH + filename, target_size=(224, 224))
        x = image.img_to_array(img)
        x = mobilenet.preprocess_input(x)
        X_val.append(x)
    print 'Total val examples: ' + str(len(y_val))
    print 'No hate examples: ' + str(total_no_hate)
    print 'Hate examples: ' + str(total_hate)

    X_train = np.array(X_train)
    y_train = np.array(y_train)
    X_val = np.array(X_val)
    y_val = np.array(y_val)
    return X_train, y_train, X_val, y_val
示例#2
0
def predict():

    # POST
    if request.method == 'POST':

        # ファイルが読み込まれていない場合は'/predict'に戻る
        if 'file' not in request.files:
            flash('No file.')
            return redirect(url_for('predict'))

        # ファイルが読み込まれている場合はそのファイルを読み込む
        file = request.files['file']
        if file.filename == '':
            flash('No file.')
            return redirect(url_for('predict'))

        # 読み込んだファイルを処理する
        if file and is_allowed_file(file.filename):

            # 安全なファイル名を作成して画像ファイルを保存
            filename = secure_filename(file.filename)
            #filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
            filepath = filename
            file.save(filepath)

            # 学習済みのMobileNetをロード
            # 構造とともに学習済みの重みも読み込まれる
            model = MobileNet(weights='imagenet')
            # model.summary()

            # 引数で指定した画像ファイルを読み込む
            # サイズはVGG16のデフォルトである224x224にリサイズされる
            img = image.load_img(filepath, target_size=(224, 224))

            # 画像ファイルをサーバから削除
            os.remove(filepath)

            # 読み込んだPIL形式の画像をarrayに変換
            x = image.img_to_array(img)

            # 3次元テンソル(rows, cols, channels) を
            # 4次元テンソル (samples, rows, cols, channels) に変換
            # 入力画像は1枚なのでsamples=1でよい
            x = np.expand_dims(x, axis=0)

            # Top-5のクラスを予測する
            # VGG16の1000クラスはdecode_predictions()で文字列に変換される
            pred = model.predict(preprocess_input(x))
            top = decode_predictions(pred, top=5)[0]
            scores = pred[0]

            # 予測結果をリストに格納する
            results = []
            for i in range(5):
                score_rounddown = int(top[i][2]*1000000) / 10000.0
                results.append([top[i][1], score_rounddown])

            return render_template('result.html', results=results)

    return render_template('predict.html')
 def predict(self, images, top=None):
     if top is None:
         top = self.num_classes
     images = preprocess_input(images)
     predictions = self.model.predict(images)
     labels = decode_predictions(predictions, top=top)
     return labels
示例#4
0
def get_image_set(type, image_limit, preproc_type='keras'):
    from keras.applications.mobilenet import preprocess_input
    from r03_mobilenet_v1_reduce_and_scale_model import process_single_item
    from a01_oid_utils import read_single_image, DATASET_PATH

    input_size = 128
    X_test, Y_test = load_oid_data_optimal(type)
    condition1 = (Y_test == 0)
    print(X_test.shape, Y_test.shape)
    X_test = np.concatenate((
        X_test[condition1][:image_limit // 2],
        X_test[~condition1][:image_limit // 2],
    ))
    Y_test = np.concatenate((
        Y_test[condition1][:image_limit // 2],
        Y_test[~condition1][:image_limit // 2],
    ))

    print(X_test.shape)
    uni = np.unique(Y_test, return_counts=True)
    print('Targets: {}'.format(uni))

    img_list = []
    for i in range(len(X_test)):
        img = process_single_item(X_test[i], input_size)
        img_list.append(img)
    img_list = np.array(img_list, dtype=np.float32)
    if preproc_type == 'keras':
        img_list = preprocess_input(img_list)
    else:
        img_list = preproc_input_mathmodel(img_list)
    print("Image limit: {} Images shape: {}".format(image_limit,
                                                    img_list.shape))
    return img_list, Y_test
    def __data_generation(self, list_IDs_temp):
        X = np.empty((self.batch_size, 1024, 1024, self.n_channels))
        y = np.empty((self.batch_size, 1024, 1024, self.n_channels_label))

        for index, id in enumerate(list_IDs_temp):
            # store image array
            seed = np.random.randint(10000000)
            temp = preprocess_input(
                image.img_to_array(image.load_img(path.join(self.img_path, id)))
            )
            temp = cv2.resize(temp, (0, 0), fx=0.5, fy=0.5)

            if self.augmentation:
                temp = self.image_gen.random_transform(temp, seed=seed)

            X[
                index,
            ] = temp

            temp = image.img_to_array(
                image.load_img(path.join(self.mask_path, self.labels[id]))
            )
            temp = cv2.resize(temp, (0, 0), fx=0.5, fy=0.5)
            if self.augmentation:
                temp = self.image_gen.random_transform(temp, seed=seed)

            temp = (
                cv2.cvtColor(temp, cv2.COLOR_RGB2GRAY).astype(bool).astype(np.float64)
            )
            temp = np.expand_dims(temp, axis=2)
            y[
                index,
            ] = temp
        return X, y
示例#6
0
def load_data_batch(num_images_total=None):
    """
    load data and preprocess before feeding it to Keras model
    :param num_images_total:
    :return:
    """

    list_x, list_y = [], []

    if num_images_total is None:
        image_names_select = img_names
    else:
        image_names_select = np.random.choice(img_names,
                                              num_images_total,
                                              replace=False)

    for img_name in image_names_select:
        x, y = get_data_sample(img_name=img_name, yn_interactive_plot=False)
        list_x.append(x)
        list_y.append(y)

    x_batch = np.stack(list_x, axis=0)
    y_batch = np.stack(list_y, axis=0)

    x_batch_ready = preprocess_input(x_batch.copy())
    y_batch_ready = np.array(y_batch, dtype='float32')

    return x_batch_ready, y_batch_ready
    def predict(self):

        K.reset_uids()

        classes = ['Normal', 'Neumonia']

        modelo = 'neumonia/model/model_neumonia_v41.json'
        pesos = 'neumonia/model/weights_neumonia_v41.h5'

        with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
            with open(modelo, 'r') as f:
                model = model_from_json(f.read())
                model.load_weights(pesos)

        img = image.load_img(self.img, target_size=(150, 150))
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        preds = model.predict(x)
        resultado = preds[0]  #solo una dimension

        porcentaje = np.round(resultado * 100, 2)
        porcentaje = list(porcentaje)

        respuesta = np.argmax(resultado)  # el valor mas alto de resultado

        for i in range(len(classes)):
            res = classes[i]

            if i == respuesta:
                return 'Resultado: {:.4}% {}'.format(round(max(porcentaje), 2),
                                                     res)
def load_images(directory, start, end):
    """
    directory配下にある犬、猫の画像を、start~endの番号分だけ読み込んで返す。
    :param directory:画像ディレクトリ
    :param start:開始番号
    :param end:終了番号
    :return:画像リストとラベルリストのNumpy配列 ラベルは猫:0、犬:1
    """
    labels = []
    imgs = []

    # 猫と犬ごとに、start~endの番号の画像を読み込む。
    for animal in ['cat', 'dog']:
        for i in range(start, end):
            # 画像パスを得る。ファイル名はcat.{番号}.jpg、またはdog.{番号}.jpg である。
            filename = '{}.{}.jpg'.format(animal, i)
            path = os.path.join(directory, filename)

            # 学習/評価用に加工する。
            img = image.load_img(path, target_size=(224, 224))
            x = image.img_to_array(img)
            x = preprocess_input(x)
            imgs.append(x)

            # 猫なら0、犬なら1をラベルに追加する。
            label = 0 if animal == 'cat' else 1
            labels.append(label)

    # Numpy配列形式で返す。
    return np.array(imgs), np.array(labels)
示例#9
0
def convertFrame(frame):
    # print('covertFrame')
    # print(frame)
    x = image.img_to_array(frame)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    return x
 def pre_classify(self, img) -> (str, bool):
     """
     Take location of image and return if it's a human, cat, dog, or neither.
     Example output:
     This looks like a human, I'll pretend it's a dog.
     This looks like a cat, I'll pretend it's a dog.
     I don't see a dog, cat, or a human in this photo... are you trying to trick me?
     :param img:
     :return:
     """
     # Convert image to 224 * 224
     img = cv2.resize(img, dsize=(224, 224))
     # Preprocess data
     img = np.expand_dims(img, axis=0)
     # Preprocess input for mobilenet
     img = mobilenet.preprocess_input(img)
     # generate predictions
     prediction = self.start_model.predict(img)
     # Class
     pred_class = self.classes[np.argmax(prediction)]
     # Response dictionary
     response_dict = dict([("dog", ("This looks like a dog, let me guess the breed.", True)),
                           ("cat", ("This looks like a cat, I'll pretend it's a dog.", True)),
                           ("person", ("This looks like a person, I'll pretend it's a dog.", True)),
                           ("other", ("I don't see a dog, cat, or a human in this photo... "
                                      "are you trying to trick me?", False))])
     return response_dict[pred_class]
示例#11
0
def assessPicture(imgPath):
    #路徑要改,要用絕對路徑
    path = imgDirectoryPath
    imgPath = os.path.join(path, imgPath)
    with tf.device('/CPU:0'):
        base_model = MobileNet((None, None, 3),
                               alpha=1,
                               include_top=False,
                               pooling='avg',
                               weights=None)
        x = Dropout(0.75)(base_model.output)
        x = Dense(10, activation='softmax')(x)
        model = Model(base_model.input, x)
        #路徑要改,要用絕對路徑
        model.load_weights(h5WeightPath)
        img = load_img(imgPath, target_size=(224, 224))
        x = img_to_array(img)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)
        scores = model.predict(x, batch_size=1, verbose=0)[0]
        mean = mean_score(scores)
        std = std_score(scores)

        print()
        print("Evaluating : ", imgPath)
        print("NIMA Score : %0.3f +- (%0.3f)" % (mean, std))
        print()
        return mean
示例#12
0
    def call_predict(self, images, folder):

        predictions = []
        #predict
        for image_name in images:
            image_path = folder + "/" + image_name
            print(f"imagepath: {image_path}")
            test_image = image.load_img(image_path,
                                        target_size=(224, 224),
                                        grayscale=False)
            test_image = image.img_to_array(test_image)
            test_image = np.expand_dims(test_image, axis=0)
            test_image = preprocess_input(test_image)
            # print(test_image)
            predict = self.model_final.predict(test_image)

            zip_pred = zip(predict[0], self.labels)

            # if the prediction is high then only senf the value
            match_found = False
            max_pred = max(zip_pred, key=lambda x: x[0])
            if (max_pred[0] > 0.3):
                match_found = True
                predictions.append((image_name, max_pred[1]))

            if (not (match_found)):
                predictions.append((image_name, ""))

        return predictions
示例#13
0
def get_test_data_mobilenet():
    print '*****TEST*****'
    filenames = os.listdir(TEST_PATH)
    shuffle(filenames)
    X_test = []
    y_test = []
    total_hate = 0
    total_no_hate = 0
    for filename in filenames:
        if "no_hate" in filename:
            y_test.append(0)
            total_no_hate += 1
        else:
            y_test.append(1)
            total_hate += 1
        img = image.load_img(TEST_PATH + filename, target_size=(224, 224))
        x = image.img_to_array(img)
        x = mobilenet.preprocess_input(x)
        X_test.append(x)
    print 'Total val examples: ' + str(len(y_test))
    print 'No hate examples: ' + str(total_no_hate)
    print 'Hate examples: ' + str(total_hate)
    X_test = np.array(X_test)
    y_test = np.array(y_test)
    return X_test, y_test
def load_data(train_dir):
    images, labels = [], []
    for i, image_name in enumerate(sorted(os.listdir(train_dir))):
        img = image.load_img(os.path.join(train_dir, image_name),
                             target_size=[224, 224])
        img = image.img_to_array(img)
        rand_seed = i % 2
        if rand_seed == 0:
            img = part_img(img, i)
            if i % 500 == 0:
                cv2.imwrite(image_name, img)
            labels.append(0)
        else:
            labels.append(1)
        if img is None:
            print image_name
        img = np.expand_dims(img, axis=0)
        img = preprocess_input(img)
        images.append(img[0])

    img_cnt = len(labels)
    shuffle_idxes = range(img_cnt)
    shuffle(shuffle_idxes)
    shuffle_imgs = list()
    shuffle_labels = list()
    for idx in shuffle_idxes:
        shuffle_imgs.append(images[idx])
        shuffle_labels.append(labels[idx])
    images = np.array(shuffle_imgs)
    labels = to_categorical(shuffle_labels)
    return images, labels
示例#15
0
def bytes_to_numpy_array(content):
    """
    Converts the image bytes (from the WhatsApp request) to a numpy array

    Args:
    content (request) : Request  received from whatsapp

    Return:
        image_array - numpy array of the image
    """
    image_url = content.get("MediaUrl0")
    # Retreive the image url
    data = requests.get(image_url).content
    # Get the image bytes
    bytesobj = io.BytesIO(data)
    # Open the bytes obj as a PIL image
    img = Image.open(bytesobj)
    # Resize the image for the CNN input_shape
    img = img.resize((224, 224), Image.ANTIALIAS)
    # get the image as a numpy array
    image_array = (np.array(img.getdata()).astype(np.float32).reshape(
        (img.size[0], img.size[1], 3)))
    # expand dims so that it's a tensor
    image_array = np.expand_dims(image_array, axis=0)
    # Apply the imagenet pre-processing
    image_array = preprocess_input(image_array)
    return image_array
示例#16
0
def predict():
    # TODO ugly handling of temp files.
    temp_file = "static/temp/upload.jpg"
    if request.method == 'POST':
        file = request.files['file']
        img_bytes = file.read()
        with open(temp_file, mode="wb") as jpg:
            jpg.write(img_bytes)
        # ImageNet prediction
        class_id, class_name = get_prediction(image_bytes=img_bytes)
        garbage_type, co = garbage_class(class_name)
        # Waste model prediction
        img = load_img(temp_file, target_size=(224, 224))
        #img = img_to_array(img) / 255.
        img = np.expand_dims(img, axis=0)
        img = preprocess_input(img)
        with graph.as_default():
            waste_model._make_predict_function()
            waste_pred = waste_model.predict(img)[0]
        index = np.argmax(waste_pred)
        waste_label = waste_types[index]
        accuracy = "{0:.2f}".format(waste_pred[index] * 100)

        return render_template('result.html',
                               class_name=class_name,
                               garbage_type=garbage_type,
                               co=co,
                               waste_label=waste_label,
                               waste_acc=accuracy)

    if os.path.exists(temp_file):
        os.remove(temp_file)  # this deletes the file

    return render_template('index.html')
def df_to_image_array_xd(df, size, lw=6, time_color=True):
    df['drawing'] = df['drawing'].apply(ast.literal_eval)
    x = np.zeros((len(df), size, size, 1))
    for i, raw_strokes in enumerate(df.drawing.values):
        x[i, :, :, 0] = draw_cv2(raw_strokes, size=size, lw=lw, time_color=time_color)
    x = preprocess_input(x).astype(np.float32)
    return x
示例#18
0
def pp_image():
    img = image.load_img('pic.png', target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    return np.asarray(x)
示例#19
0
    def test_01_image_classifier_with_image_as_input(self):
        model = applications.MobileNet(weights='imagenet',
                                       include_top=False,
                                       input_shape=(224, 224, 3))
        activType = 'sigmoid'
        x = model.output
        x = Flatten()(x)
        x = Dense(1024, activation="relu")(x)
        predictions = Dense(2, activation=activType)(x)
        model_final = Model(inputs=model.input,
                            outputs=predictions,
                            name='predictions')

        cnn_pmml = KerasToPmml(model_final,model_name="MobileNetImage",description="Demo",\
            copyright="Internal User",dataSet='image',predictedClasses=['dogs','cats'])
        cnn_pmml.export(open('2classMBNet.pmml', "w"), 0)

        img = image.load_img('nyoka/tests/resizedCat.png')
        img = img_to_array(img)
        img = preprocess_input(img)
        imgtf = np.expand_dims(img, axis=0)
        model_pred = model_final.predict(imgtf)
        model_preds = {'dogs': model_pred[0][0], 'cats': model_pred[0][1]}

        model_name = self.adapa_utility.upload_to_zserver('2classMBNet.pmml')

        predictions, probabilities = self.adapa_utility.score_in_zserver(
            model_name, 'nyoka/tests/resizedCat.png', 'DN')

        self.assertEqual(
            abs(probabilities['cats'] - model_preds['cats']) < 0.00001, True)
        self.assertEqual(
            abs(probabilities['dogs'] - model_preds['dogs']) < 0.00001, True)
示例#20
0
def predict(photo):
    photo = cv.resize(photo, (224, 224))
    photo = image.img_to_array(photo)
    photo = preprocess_input(photo)
    photo = np.expand_dims(photo, axis=0)
    pred = model.predict(photo)
    return pred
    def load_file(self, filename):
        try:
            image_cropper = ZPILImageCropper(filename)

            # 1 Crop larget_central square region from an image of filename.
            cropped_image = image_cropper.crop_largest_central_square_region()

            # 2 Load an image from the cropped_fle.
            self.image_view.set_image(img_to_array(cropped_image))
            self.image_view.update_scale()

            self.set_filenamed_title(filename)

            # 3 Resize the cropped_image
            self.image = cropped_image.resize(self.image_size)

            # 4 Convert the self.image to numpy ndarray and remove alpha channel.
            self.image = self.remove_alpha_channel(img_to_array(self.image))

            # 5 Set self.nadarryy to the test_image_view.
            self.test_image_view.set_image(self.image)

            # 6 Convert self.image(ndarray) to an input data format applicable to the VGG16 model.
            self.image = preprocess_input(self.image)

        except:
            self.write(formatted_traceback())
    def extract(self, img):

        if type(img) is str:
            img = image.load_img(img, target_size=(224, 224))
            x = image.img_to_array(img)
        else:
            x = scipy.misc.imresize(img, (224, 224))

            # Convert type to float
            x = x.astype(float)

        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
示例#23
0
def model_predict(img_path, model):
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)  # Preprocessing the image
    x = np.expand_dims(x, axis=0)  # x = np.true_divide(x, 255)

    preds = model.predict(preprocess_input(x))
    return preds
示例#24
0
def upload():
    target = os.path.join(APP_ROOT, 'static/xray/')
    if not os.path.isdir(target):
        os.mkdir(target)
    filename = ""
    for file in request.files.getlist("file"):
        filename = file.filename
        destination = "/".join([target, filename])
        file.save(destination)

    image = cv2.resize(
        cv2.cvtColor(cv2.imread(destination), cv2.COLOR_BGR2RGB), (224, 224))
    image1 = preprocess_input(image)
    image1 = np.expand_dims(image1, axis=0)
    with graph.as_default():
        pred = model.predict(image1)
    prediction = int(np.argmax(pred, axis=1)[0])
    if prediction:
        plot_dest = "/".join([target, filename + "result.png"])
        mob_model, all_amp_layer_weights = get_mobileNet(model)
        fig, ax = plt.subplots()
        plot_mobilenet_CAM(image, image1, ax, mob_model, all_amp_layer_weights)
        plt.savefig(plot_dest, bbox_inches='tight')

    return render_template("result.html",
                           prediction=prediction,
                           filename=filename,
                           plot_dest=filename + "result.png")
示例#25
0
def label_image(image, model, verbose=False):
    # Reference: https://github.com/glouppe/blackbelt/

    # convert the image pixels to a numpy array
    image = img_to_array(image)

    # reshape data for the model
    image = np.expand_dims(image, axis=0)

    # prepare the image for the VGG model
    image = preprocess_input(image)

    # predict the probability across all output classes
    yhat = model.predict(image)

    if verbose and model.output_names[0] == 'predictions':
        # convert the probabilities to class labels
        labels = decode_predictions(yhat)

        # retrieve the most likely result, e.g. highest probability
        label = labels[0][0]

        # print the classification
        print('%s (%.2f%%)' % (label[1], label[2] * 100))

    return yhat
示例#26
0
def predictImage(request):
    fileObj = request.FILES['filePath']
    fs = FileSystemStorage()
    filePathName = fs.save(fileObj.name, fileObj)
    filePathName = fs.url(filePathName)
    test_image = '.' + filePathName
    img = image.load_img(test_image, target_size=(img_height, img_width))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    with model_graph.as_default():
        with tf_session.as_default():
            predi = model.predict(x)

    p_good, p_ill = np.around(predi, decimals=2)[0]
    predictedLabel = [p_good, p_ill]
    p_good = p_good * 100
    p_ill = p_ill * 100

    if p_ill > p_good:
        predictedLabel = ' Chances of pneumonia is high with ' + str(
            p_ill) + ' %'
    else:
        predictedLabel = ' Chances of pneumonia is low with ' + str(
            p_ill) + ' %'
    context = {'filePathName': filePathName, 'predictedLabel': predictedLabel}
    return render(request, 'index.html', context)
示例#27
0
    def predict_classes(self,
                        path_input,
                        filename_output,
                        limit=1000000,
                        mask='*.png'):

        patterns = numpy.sort(
            numpy.array([
                f.path[len(path_input):] for f in os.scandir(path_input)
                if f.is_dir()
            ]))

        for each in patterns:
            print(each)
            local_filenames = numpy.array(
                fnmatch.filter(listdir(path_input + each), mask))[:limit]
            for i in range(0, local_filenames.shape[0]):
                img = cv2.imread(path_input + each + '/' + local_filenames[i])
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
                img = cv2.resize(img, self.input_shape).astype(numpy.float32)

                model = Model(inputs=self.model.input,
                              outputs=self.model.output)
                prob = model.predict(preprocess_input(numpy.array([img])))[0]

                idx = numpy.argsort(-prob)[0]
                label = self.class_names[idx]
                tools_IO.save_labels(path_input + each + '/' + filename_output,
                                     numpy.array([local_filenames[i]]),
                                     numpy.array([label]),
                                     append=i,
                                     delim=' ')

        return
示例#28
0
def record_bottleneck(s3_bucket, s3_key, pretrained_model):
    # load selected pre-trained model
    if pretrained_model == 'MobileNet':
        from keras.applications.mobilenet import MobileNet, preprocess_input
        model = MobileNet(input_shape=(224, 224, 3),
                          include_top=False,
                          weights='imagenet')

    img_path = '/tmp/{}'.format(s3_key)  # where to download image
    feat_path = '/tmp/{}.npy'.format(s3_key)  # where to save features

    # download file to /tmp/[s3_key]
    s3 = boto3.client('s3')
    s3.download_file(Bucket=s3_bucket, Key=s3_key, Filename=img_path)

    # load and preprocess image
    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)

    # get bottleneck features
    features = model.predict(x)

    # save features to /tmp/[s3_key].npy
    np.save(open(feat_path, 'wb'), features)

    # upload features to s3://[s3_bucket]/[s3_key].npy
    s3.upload_file(Filename=feat_path, Bucket=s3_bucket, Key=s3_key + '.npy')
def get_rez(pic):
    img = image.load_img(pic, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    p_good, p_ill = np.around(new_model.predict(x), decimals=2)[0]
    return {'p_good': p_good, 'p_ill': p_ill}
示例#30
0
    def make_prediction(self):
        K.reset_uids()
        model = ""
        weights = ""
        classes = {
            'TRAIN': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'],
            'VALIDATION': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'],
            'TEST': ['GRADE 0', 'GRADE 1', 'GRADE 2', 'GRADE 3', 'GRADE 4'],
        }    
        
        with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
            with open(model, 'r') as f:
                model = model_from_json(f.read())
                model.load_weights(weights)

        xray_image = image.load_img(self.img, target_size=(224, 224))
        x = image.img_to_array(xray_image)
        x = np.expand_dims(x, axis=0)
        x = preprocess_input(x)

        model.compile(loss="categorical_crossentropy", metrics=[
                      "accuracy"], optimizer="adam")
        result = model.predict(x)

        pred_name = classes['TRAIN'][np.argmax(result)]

        messages.success(result, f"The osteoarthritis is predicted to be {pred_name}".format(pred_name))

        return pred_name
def preprocess_image(image, target_size):
    if image.mode != "L":
        image = image.convert("L")
    image = image.resize(target_size)
    image_arr = img_to_array(image)
    image_arr = np.expand_dims(image_arr, axis=0)
    image_arr = preprocess_input(image_arr)
    return image_arr, image
示例#32
0
def mnetv2_loadimage(image_path):
    img = image.load_img(image_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    return preprocess_input(x)