Example #1
0
        [SliceChannels(192, 384), layers.Convolution((128, 3, 3), padding=1, name="conv_5_2"), layers.Relu()],
    ],
    layers.Concatenate(),
    layers.MaxPooling((3, 3), stride=(2, 2)),
    layers.Reshape(),
    layers.Relu(4096, name="dense_1") > layers.Dropout(0.5),
    layers.Relu(4096, name="dense_2") > layers.Dropout(0.5),
    layers.Softmax(1000, name="dense_3"),
)

if not os.path.exists(ALEXNET_WEIGHTS_FILE):
    download_file(
        url=("http://srv70.putdrive.com/putstorage/DownloadFileHash/" "F497B1D43A5A4A5QQWE2295998EWQS/alexnet.pickle"),
        filepath=ALEXNET_WEIGHTS_FILE,
        description="Downloading weights",
    )

storage.load(alexnet, ALEXNET_WEIGHTS_FILE)

dog_image = load_image(
    os.path.join(CURRENT_DIR, "images", "dog.jpg"), image_size=(256, 256), crop_size=(227, 227), use_bgr=False
)

# Disables dropout layer
with alexnet.disable_training_state():
    x = T.tensor4()
    predict = theano.function([x], alexnet.output(x))

output = predict(dog_image)
print_top_n(output[0], n=5)
Example #2
0

def prepare_image(fname):
    with open(IMAGENET_MEAN_FILE, 'rb') as f:
        # Mean values is the average image accros all training dataset.
        # if dataset (1000, 3, 224, 224) then mean image shape
        # is (3, 224, 224) and computes as data.mean(axis=0)
        mean_values = pickle.load(f)

    image = read_image(fname, image_size=(256, 256), crop_size=(224, 224))
    # Convert RGB to BGR
    image[:, (0, 1, 2), :, :] = image[:, (2, 1, 0), :, :]
    return asfloat(image - mean_values)


environment.speedup()
resnet50 = architectures.resnet50()

if not os.path.exists(RESNET50_WEIGHTS_FILE):
    download_file(
        url="http://neupy.s3.amazonaws.com/imagenet-models/resnet50.pickle",
        filepath=RESNET50_WEIGHTS_FILE,
        description='Downloading weights')

storage.load(resnet50, RESNET50_WEIGHTS_FILE)
predict = resnet50.compile()

dog_image = prepare_image(DOG_IMAGE_PATH)
output = predict(dog_image)
print_top_n(output, n=5)
Example #3
0
    layers.Convolution((512, 3, 3), padding=1, name='conv4_1') > layers.Relu(),
    layers.Convolution((512, 3, 3), padding=1, name='conv4_2') > layers.Relu(),
    layers.Convolution((512, 3, 3), padding=1, name='conv4_3') > layers.Relu(),
    layers.MaxPooling((2, 2)),
    layers.Convolution((512, 3, 3), padding=1, name='conv5_1') > layers.Relu(),
    layers.Convolution((512, 3, 3), padding=1, name='conv5_2') > layers.Relu(),
    layers.Convolution((512, 3, 3), padding=1, name='conv5_3') > layers.Relu(),
    layers.MaxPooling((2, 2)),
    layers.Reshape(),
    layers.Relu(4096, name='dense_1') > layers.Dropout(0.5),
    layers.Relu(4096, name='dense_2') > layers.Dropout(0.5),
    layers.Softmax(1000, name='dense_3'),
)

if not os.path.exists(VGG16_WEIGHTS_FILE):
    download_file(url=("http://srv70.putdrive.com/putstorage/DownloadFileHash/"
                       "5B7DCBF43A5A4A5QQWE2301430EWQS/vgg16.pickle"),
                  filepath=VGG16_WEIGHTS_FILE,
                  description='Downloading weights')

storage.load(vgg16, VGG16_WEIGHTS_FILE)

dog_image = load_image(os.path.join(CURRENT_DIR, 'images', 'dog.jpg'),
                       image_size=(256, 256),
                       crop_size=(224, 224))

predict = vgg16.compile()
output = predict(dog_image)

print_top_n(output[0], n=5)