示例#1
0
def imread_scale(img_path, shape=input_shape, preprocess=input_preprocess):
    if preprocess:
        return densenet.preprocess_input(
            cv2.resize(imread(img_path),
                       (shape[1], shape[0])).astype(np.float64))
    else:
        return cv2.resize(imread(img_path),
                          (shape[1], shape[0])).astype(np.float64)
def infer(model, image_path):
    img = image.load_img(image_path, target_size=(SIZE, SIZE))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    preds_with_labels = decode_predictions(preds)
    return dict((t[1], t[2]) for t in preds_with_labels[0])
示例#3
0
def Main():
    font = cv2.FONT_HERSHEY_SIMPLEX
    size = 0.5
    fx = 10
    fy = 10
    fh = 18

    ## Grab camera input
    cap = cv2.VideoCapture(0)
    # cv2.namedWindow('Original', cv2.WINDOW_NORMAL)

    # set rt size as 640x480
    ret = cap.set(3, img_cols)
    ret = cap.set(4, img_rows)

    framecount = 0
    fps = ""
    start = time.time()
    model = trainable_lyz.buildmodel()
    model = trainable_lyz.load_weights(model)

    while (True):
        ret, frame = cap.read()
        frame = cv2.flip(frame, 3)
        frame = cv2.resize(frame, (img_cols, img_rows))

        if ret == True:
            framecount = framecount + 1
            end = time.time()
            timediff = (end - start)
            if (timediff >= 1):
                fps = 'FPS:%s' % (framecount)
                start = time.time()
                framecount = 0

            if framecount % 5 == 0:
                img = frame.astype('float32')
                img = densenet.preprocess_input(img)
                y, prob, class_name = trainable_lyz.predict(
                    model, img, class_names)
                cv2.putText(frame,
                            str(class_name) + ': ' + str(prob), (20, 20), font,
                            0.7, (0, 255, 0), 2, 1)

            cv2.imshow('video1', frame)
            cv2.putText(frame, fps, (10, 20), font, 0.7, (0, 255, 0), 2, 1)
            cv2.putText(frame, 'ESC - Exit', (fx, fy), font, size, (0, 255, 0),
                        1, 1)
        ############## Keyboard inputs ##################
        ## Use Esc key to close the program
        key = cv2.waitKey(5) & 0xff
        if key == 27:
            break

    #Realse & destroy
    cap.release()
    cv2.destroyAllWindows()
示例#4
0
def loaddata():
    print('loading data...')
    # (trainX, trainY), (testX, testY) = cifar10.load_data()
    trainX, trainY, testX, testY, class_names = process_img_lyz.load_js_data(path=path, img_rows=img_rows, img_cols=img_cols)
    # img = Image.fromarray(trainX[0]).convert('RGB')
    # plt.imshow(img)
    # plt.show()
    # exit(0)

    trainX = trainX.astype('float32')
    testX = testX.astype('float32')

    trainX = densenet.preprocess_input(trainX)
    testX = densenet.preprocess_input(testX)

    trainY = np_utils.to_categorical(trainY, nb_classes)
    testY = np_utils.to_categorical(testY, nb_classes)
    print('data loaded.')
    return trainX, trainY, testX, testY, class_names 
def extract_DenseNet(path):
    import densenet
    from keras.preprocessing import image
    import numpy as np
    img = image.load_img(path, target_size=(224, 224))
    x = image.img_to_array(img)
    img_tensor = densenet.preprocess_input(np.expand_dims(x, axis=0))
    image_dim = (224, 224, 3)
    DenseNet_model = densenet.DenseNetImageNet161(input_shape=image_dim,
                                                  include_top=False)
    DenseNet_output = DenseNet_model.predict(img_tensor)
    return DenseNet_output
示例#6
0
def predict(path, model_path, index_file_path, MainUI):

    try:
        result_string = ""



        image_to_predict = image.load_img(path,
                                          grayscale=False, target_size=(224, 224))
        image_to_predict = image.img_to_array(image_to_predict, data_format="channels_last")
        image_to_predict = np.expand_dims(image_to_predict, axis=0)

        image_to_predict = preprocess_input(image_to_predict, data_format="channels_last")

        if (MainUI.densenet_model_loaded == False):
            wx.CallAfter(pub.sendMessage, "report101",
                         message="Loading DenseNet model for the first time. This may take few minutes or less than a minute. Please wait. \nLoading.....")
            model = DenseNetImageNet121(input_shape=(224, 224, 3), model_path=model_path )
            wx.CallAfter(pub.sendMessage, "report101",
                         message="DenseNet model loaded.. Picture about to be processed.. \nLoading......")
            MainUI.model_collection_densenet.append(model)
            MainUI.densenet_model_loaded = True
        else:
            wx.CallAfter(pub.sendMessage, "report101", message="Retrieving loaded model. \nLoading........")
            model = MainUI.model_collection_densenet[0]
            wx.CallAfter(pub.sendMessage, "report101",
                        message="DenseNet model loaded.. Picture about to be processed.. \nLoading......")

        wx.CallAfter(pub.sendMessage, "report101", message="Picture is transformed for prediction. \nLoading........")
        prediction = model.predict(x=image_to_predict, steps=1)
        wx.CallAfter(pub.sendMessage, "report101",
                     message="Picture prediction is done. Sending in results. \nLoading......")

        prediction_result = decode_predictions(prediction, top=10, index_file_path=index_file_path)

        for results in prediction_result:
            countdown = 0
            for result in results:
                countdown += 1
                result_string += "(" + str(countdown) + ") " + str(result[1]) + " : " + str(100 * result[2])[
                                                                            0:4] + "% \n"
        return result_string
    except Exception as e:
        return getattr(e, "message", repr(e))
示例#7
0
                          growth_rate=growth_rate, nb_filter=nb_filter, dropout_rate=dropout_rate, weights=None,
                          bottleneck=False)
print("Model created")

# model.summary()
optimizer = Adam(lr=1e-3)  # Using Adam instead of SGD to speed up training
model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=["accuracy"])
print("Finished compiling")
print("Building model...")

(trainX, trainY), (testX, testY) = cifar10.load_data()

trainX = trainX.astype('float32')
testX = testX.astype('float32')

trainX = densenet.preprocess_input(trainX)
testX = densenet.preprocess_input(testX)

Y_train = np_utils.to_categorical(trainY, nb_classes)
Y_test = np_utils.to_categorical(testY, nb_classes)

generator = ImageDataGenerator(rotation_range=15,
                               width_shift_range=5. / 32,
                               height_shift_range=5. / 32,
                               horizontal_flip=True)

# Here we zip the data and its classes so they get moved around together
train = zip(trainX, trainY)

# Then we sort the zipped list by its classes
train = sorted(train, key=lambda x: x[1])
示例#8
0
def train1():
    batch_size = 20
    nb_classes = 30
    nb_epoch = 10

    img_rows, img_cols = 64, 64
    img_channels = 3

    img_dim = (img_channels, img_rows,
               img_cols) if K.image_dim_ordering() == "th" else (img_rows,
                                                                 img_cols,
                                                                 img_channels)
    depth = 40
    nb_dense_block = 3
    growth_rate = 12
    nb_filter = -1
    dropout_rate = 0.0  # 0.0 for data augmentation

    model = densenet.DenseNet(img_dim,
                              classes=nb_classes,
                              depth=depth,
                              nb_dense_block=nb_dense_block,
                              growth_rate=growth_rate,
                              nb_filter=nb_filter,
                              dropout_rate=dropout_rate,
                              weights=None)
    print("Model created")

    model.summary()
    optimizer = Adam(lr=1e-3)  # Using Adam instead of SGD to speed up training
    model.compile(loss='categorical_crossentropy',
                  optimizer=optimizer,
                  metrics=["accuracy"])
    print("Finished compiling")
    print("Building model...")

    train_file_path = r'../DatasetA_train_20180813/train/'
    com_path = r'../data/com.txt'

    trainX, trainY, testX, testY = load_data(train_file_path, com_path)

    print(trainX.shape)
    # print(trainY.shape)
    print(testX.shape)
    # print(testY.shape)

    trainX = trainX.astype('float32')
    testX = testX.astype('float32')

    trainX = densenet.preprocess_input(trainX)
    testX = densenet.preprocess_input(testX)

    Y_train = np_utils.to_categorical(trainY, nb_classes)
    Y_test = np_utils.to_categorical(testY, nb_classes)

    generator = ImageDataGenerator(rotation_range=15,
                                   width_shift_range=5. / 32,
                                   height_shift_range=5. / 32,
                                   horizontal_flip=True)

    generator.fit(trainX, seed=0)

    # Load model
    weights_file = "weights/DenseNet-40-12-CIFAR10.h5"
    if os.path.exists(weights_file):
        # model.load_weights(weights_file, by_name=True)
        print("Model loaded.")

    out_dir = "weights/"

    lr_reducer = ReduceLROnPlateau(monitor='val_acc',
                                   factor=np.sqrt(0.1),
                                   cooldown=0,
                                   patience=5,
                                   min_lr=1e-5)
    model_checkpoint = ModelCheckpoint(weights_file,
                                       monitor="val_acc",
                                       save_best_only=True,
                                       save_weights_only=True,
                                       verbose=1)

    callbacks = [lr_reducer, model_checkpoint]

    model.fit_generator(generator.flow(trainX, Y_train, batch_size=batch_size),
                        steps_per_epoch=len(trainX) // batch_size,
                        epochs=nb_epoch,
                        callbacks=callbacks,
                        validation_data=(testX, Y_test),
                        validation_steps=testX.shape[0] // batch_size,
                        verbose=1)

    yPreds = model.predict(testX)
    yPred = np.argmax(yPreds, axis=1)
    yTrue = testY

    accuracy = metrics.accuracy_score(yTrue, yPred) * 100
    error = 100 - accuracy
    print("Accuracy : ", accuracy)
    print("Error : ", error)
示例#9
0
def process_data(_features, _labels):
    _type = 'float32'
    _features = densenet.preprocess_input(np.array(_features).astype(_type))
    _labels = np.array(_labels).astype(_type)
    return _features, _labels
from __future__ import print_function
from __future__ import absolute_import

from keras.preprocessing import image

from densenet import DenseNetImageNet121, DenseNetImageNet169, DenseNetImageNet161, preprocess_input, decode_predictions

import numpy as np

if __name__ == '__main__':
    size = 224

    model = DenseNetImageNet121(input_shape=(size, size, 3))
    model.summary()


    img_path = 'images/elephant.jpg'
    img = image.load_img(img_path, target_size=(size, size))
    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))

    return a[p], b[p]


if __name__ == "__main__":
    (x_in_train, y_in_train), (x_in_test, y_in_test) = get_in_dist_train_data()
    (x_out_train, y_out_train), (x_out_test, y_out_test) = get_out_dist_train_data()

    # Concatenate in and out dist images
    x_train_raw = np.concatenate((x_in_train, x_out_train))
    y_train_raw = np.concatenate((y_in_train, y_out_train))

    x_test_raw = np.concatenate((x_in_test, x_out_test))
    y_test_raw = np.concatenate((y_in_test, y_out_test))

    # Pre-process inputs
    x_train = densenet.preprocess_input(x_train_raw)
    x_test = densenet.preprocess_input(x_test_raw)

    y_train = np_utils.to_categorical(y_train_raw, 2)
    y_test = np_utils.to_categorical(y_test_raw, 2)

    # Shuffle training data
    x_train, y_train = unison_shuffled_copies(x_train, y_train)
    x_test, y_test = unison_shuffled_copies(x_test, y_test)

    model = get_model()
    model.fit(x=x_train,
              y=y_train,
              batch_size=32,
              epochs=100,
              validation_data=(x_test, y_test))