Exemplo n.º 1
0
def classify(img_in, img_out):
    images = list()

    # load the VGG16 network
    print("[INFO] loading network...")
    model = VGG16(weights="imagenet")
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='categorical_crossentropy')

    for item in os.listdir(img_in):
        if not item.startswith('.') and os.path.isfile(
                os.path.join(img_in, item)):
            print(item)
            img = cv2.imread(os.path.join(img_in, item))

            if img is not None:
                images.append(img)
                print("[INFO] loading and preprocessing image...")
                image = cv2.resize(img, (224, 224))
                image = image_utils.img_to_array(image)
                # our image is now represented by a NumPy array of shape (3, 224, 224),
                # but we need to expand the dimensions to be (1, 3, 224, 224) so we can
                # pass it through the network -- we'll also preprocess the image by
                # subtracting the mean RGB pixel intensity from the ImageNet dataset
                image = np.expand_dims(image, axis=0)
                image = preprocess_input(image)

                # classify the image
                print("[INFO] classifying image...")
                preds = model.predict(image)
                P = decode_predictions(preds)
                (imagenetID, label, prob) = P[0][0]
                #print('Predicted:', decode_predictions(preds,1))
                print(decode_predictions(preds, 3))

                # display the predictions to our screen
                print("ImageNet ID: {}, Output label: {},  Prob: {}".format(
                    imagenetID, label, round(prob, 4)))
                cv2.putText(img, "Output label: {}".format(label), (10, 30),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.9, (102, 0, 255), 2)
                cv2.putText(img, "Prob: {0:.0f}%".format(prob * 100), (10, 90),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)

                #create a new folder
                foldoutcheck = os.path.exists(img_out)
                if foldoutcheck == False:
                    os.makedirs(img_out)
                foldcheck = os.path.exists(img_out + "/" + label)
                if foldcheck == True:
                    cv2.imwrite(img_out + "/" + label + "/" + item, img)
                else:
                    os.makedirs(img_out + "/" + label)
                    cv2.imwrite(img_out + "/" + label + "/" + item, img)
Exemplo n.º 2
0
def classify_val(model, syn_data, idx):
    img, val = get_val_img(idx, size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    preds = model.predict(x)
    # val_truth = det_synsets[label]
    clsloc_preds = decode_predictions(preds)
    det_preds = syn_data.decode_predictions_det(clsloc_preds)
    # print('Validation label: {},{},{}'.format(val_truth['id'],val_truth['wnid'],val_truth['name']))
    print('Predicted clsloc:', clsloc_preds)
    print('Predicted det:', det_preds)
    return preds, det_preds, clsloc_preds
from models.imagenet_utils import preprocess_input ,decode_predictions


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

img=image.load_img("elephent.jpeg",target_size=(224,224))

x=image.img_to_array(img)

print(type(x))
x = np.expand_dims(x,axis=0)
x=preprocess_input(x)

predict=model.predict(x)

print("prediction",decode_predictions(predict))

model.summary()


model.layers[-1].get_config()

//////////////////////////////////////////////


model=VGG16(weights='imagenet',include_top=False)
model.summary()


model.layers[-1].get_config()
Exemplo n.º 4
0
            if include_top:
                weights_path = get_file(
                    'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                    TF_WEIGHTS_PATH,
                    cache_subdir='models',
                    md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
            else:
                weights_path = get_file(
                    'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                    TF_WEIGHTS_PATH_NO_TOP,
                    cache_subdir='models',
                    md5_hash='a268eb855778b3df3c7506639542a6af')
            model.load_weights(weights_path)
            if K.backend() == 'theano':
                convert_all_kernels_in_model(model)
    return model


if __name__ == '__main__':
    model = ResNet50(include_top=True, weights='imagenet')

    img_path = 'elephant.jpg'
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)
    print('Input image shape:', x.shape)

    preds = model.predict(x)
    print('Predicted:', decode_predictions(preds))