Exemplo n.º 1
0
def predict(config_path, img_path):
    """Predict using trained FCN.

    The FCN is loaded and the images located in 'img_path' are divided
    into batches before being being labelled by the FCN.

    Args:
    """
    config = json.load(open(config_path))

    fcn = fcn_alex(config['output_weight_path'])

    batch_val = 6
    batch_num = int(120 / batch_val)

    for batch in range(batch_num):
        im_list = []
        out_list = []
        for filename in os.listdir(img_path)[batch *
                                             batch_val:batch * batch_val +
                                             batch_val]:
            #name, file_extension = filename[:].split('.')
            im_list.append('../input/' + filename[:])
            out_list.append('../output/' + filename[:].split('.')[0])
            #if file_extension == 'png' or file_extension == 'Png' or file_extension == 'jpg' or file_extension == 'Jpeg':
            #    print(filename)
            #    img = cv2.resize(cv2.imread(img_path+filename), (64, 64)).astype(np.float32)
            #    fcn.predict(img)
        print(im_list[:])
        im = preprocess_image_batch(im_list[:], color_mode="bgr")
        #im = preprocess_image_batch(im_list[:], color_mode="rgb")

        fcn.predict(im, out_list[:])
def load_train():
    X_train = []
    X_train_id = []
    y_train = []
    start_time = time.time()

    print('Read train images')
    folders = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']
    for fld in folders:
        index = folders.index(fld)
        print('Load folder {} (Index: {})'.format(fld, index))
        path = os.path.join('..', 'input', 'train', fld, '*.jpg')
        files = glob.glob(path)
        for fl in files:
            flbase = os.path.basename(fl)
            im = preprocess_image_batch([fl],
                                        img_size=(256, 256),
                                        crop_size=(227, 227),
                                        color_mode="bgr")
            out = model.predict(im)
            X_train.append(out.flatten())
            X_train_id.append(flbase)
            y_train.append(index)
    print('Read train data time: {} seconds'.format(
        round(time.time() - start_time, 2)))
    return np.array(X_train), y_train, X_train_id
Exemplo n.º 3
0
def predict(input_image, layer_dim=[], weights=None, verbose=True):
    # input: an image file, a folder containing images, a folder containing labe-folders containing images
    # model: a path to a keras json model, a name of a supported pre-build network (vgg16), a keras model

    if weights is None:
        weights = DEFAULT_WEIGHTS_FILE
    elif isinstance(weights, basestring) and os.path.isfile(weights):
        weights = h5py.File(weights)

    if not isinstance(weights, h5py.File):
        raise ValueError(
            "weights is not a valid hdf5 file or path to a valid hdf5 file.")

    im = preprocess_image_batch([input_image], color_mode="rgb")

    model = VGG16Builder.pretrainedModel(weights=weights,
                                         layer_dim=layer_dim,
                                         heatmap=True)
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model.compile(optimizer=sgd, loss='mse')

    prediction = model.predict(im)

    if verbose:
        print prediction
    return prediction
Exemplo n.º 4
0
def save_img_h5(data, h5_file, cache_size=30, img_size=None):
    f = h5py.File(h5_file, "w")
    files = [name for (name, l) in data]
    labels = np.array([l for (name, l) in data])
    n_samples = len(data)

    files_dset = f.create_dataset("files", data=files)

    lab_dset = f.create_dataset("labels", data=labels, compression="gzip")

    img_dset = f.create_dataset("imgs", (n_samples, 3, 256, 256),
                                dtype='float32',
                                compression="gzip")
    n_step = len(data) / cache_size
    if n_step % cache_size != 0:
        n_step += 1

    ############# Progressbar ################
    widgets = [Percentage(), ' ', Bar(), ' ', ETA(), ' ', AdaptiveETA()]
    pbar = ProgressBar(widgets=widgets, maxval=len(data))
    pbar.start()
    #########################################

    for j in range(n_step):
        X = preprocess_image_batch(files[j * cache_size:(j + 1) * cache_size],
                                   img_size=img_size)
        img_dset[j * cache_size:(j + 1) * cache_size] = X
        pbar.update(j * cache_size)

    pbar.finish()
    f.close()
def main(model_file, img_list, weights_path, outputFile,
         batch_size=32, batch_per_cache=None):
    imp.load_source("convnet", model_file)
    from convnet import model
    model.load_weights(weights_path)

    data = []
    with open(img_list, "r") as f:
        for l in f:
            data.append(l[:-1])

    print("File paths loaded")
    output = []
    i = 0
    #while i < len(data):
    f = open(outputFile, "w")
    for i in range(0, len(data), batch_size):
        print i
        try:
            X = preprocess_image_batch(data[i:i+batch_size],
                                       img_size=(224,224))
        except:
            print("BUG")
            continue
            
        Y_pred = model.predict_on_batch(X)
        output.extend(zip(data[i:i+batch_size], list(Y_pred)))
        for j, path_img in enumerate(data[i:i+batch_size]):
            #ipdb.set_trace()
            f.write(os.path.basename(path_img)+";")
            f.write(";".join((str(l)+";"+str(s) for (l,s) in enumerate(list(Y_pred[j])))))
            f.write("\n")

    f.close()
 def _readImageFormDirectoryNoCache(self, num):
     list_path = []
     elem = self.listDirs[num]
     list_path.append(self.pathImgs + elem + "/" +
                      os.listdir(self.pathImgs + elem)[0])
     img = preprocess_image_batch(list_path,
                                  img_size=(256, 256),
                                  crop_size=(227, 227),
                                  color_mode="rgb")
     self.listImgsClassLabel[num] = img
     self.dictOfImages[num] = elem
     return img
 def _getFullListImagesFromDirectory(self):
     i = 0
     list_path = []
     for elem in self.listDirs:
         list_path.append(self.pathImgs + elem + "/" +
                          os.listdir(self.pathImgs + elem)[0])
         img = preprocess_image_batch(list_path,
                                      img_size=(256, 256),
                                      crop_size=(227, 227),
                                      color_mode="rgb")
         self.listImgsClassLabel[i] = img
         self.dictOfImages[i] = elem
         i += 1
         list_path.clear()
     return None
def load_test():
    path = os.path.join('..', 'input', 'test_stg1', '*.jpg')
    files = sorted(glob.glob(path))

    X_test = []
    X_test_id = []
    for fl in files:
        flbase = os.path.basename(fl)
        im = preprocess_image_batch([fl],
                                    img_size=(256, 256),
                                    crop_size=(227, 227),
                                    color_mode="rgb")
        out = model.predict(im)
        X_test.append(out.flatten())
        X_test_id.append(flbase)
    return np.array(X_test), X_test_id
Exemplo n.º 9
0
def predict(path):
    fcn = FCN('model/post_alexnet_weights.h5')

    batch_val = 6
    batch_num = int(120 / batch_val)

    for batch in range(batch_num):
        im_list = []
        out_list = []
        for filename in os.listdir(path)[batch * batch_val:batch * batch_val +
                                         batch_val]:
            #name, file_extension = filename[:].split('.')
            im_list.append('input/' + filename[:])
            out_list.append('output/' + filename[:].split('.')[0])
            #if file_extension == 'png' or file_extension == 'Png' or file_extension == 'jpg' or file_extension == 'Jpeg':
            #    print(filename)
            #    img = cv2.resize(cv2.imread(path+filename), (64, 64)).astype(np.float32)
            #    fcn.predict(img)
        print(im_list[:])
        im = preprocess_image_batch(im_list[:], color_mode="bgr")
        fcn.predict(im, out_list[:])
Exemplo n.º 10
0
def main(model_file,
         img_list,
         weights_path,
         outputFile,
         batch_size=32,
         batch_per_cache=None):
    imp.load_source("convnet", model_file)
    from convnet import model
    model.load_weights(weights_path)

    data = []
    with open(img_list, "r") as f:
        for l in f:
            data.append(l[:-1])

    print("File paths loaded")
    output = []
    i = 0
    #while i < len(data):
    f = open(outputFile, "w")
    for i in range(0, len(data), batch_size):
        print i
        try:
            X = preprocess_image_batch(data[i:i + batch_size],
                                       img_size=(224, 224))
        except:
            print("BUG")
            continue

        Y_pred = model.predict_on_batch(X)
        output.extend(zip(data[i:i + batch_size], list(Y_pred)))
        for j, path_img in enumerate(data[i:i + batch_size]):
            #ipdb.set_trace()
            f.write(os.path.basename(path_img) + ";")
            f.write(";".join((str(l) + ";" + str(s)
                              for (l, s) in enumerate(list(Y_pred[j])))))
            f.write("\n")

    f.close()
Exemplo n.º 11
0
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="weights/alexnet_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)
print(out)
Exemplo n.º 12
0
import os
os.environ['KERAS_BACKEND'] = 'theano'
os.environ['THEANO_FLAGS'] = 'floatX=float32,device=gpu,lib.cnmem=0.8,dnn.conv.algo_bwd_filter=deterministic,dnn.conv.algo_bwd_data=deterministic,blas.ldflags=-LC:/toolkits/openblas-0.2.14-int32/bin -lopenblas'

from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
import numpy as np
DATAPATH = "../USNS/RawData/train/"
imgNames = [DATAPATH+fname for fname in os.listdir(DATAPATH) if "mask" not in fname]

x_train = preprocess_image_batch(imgNames,img_size=(256,256), crop_size=(227,227), color_mode="rgb")
print "Pre-processing done"
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',weights_path="../USNS/AlexNet/weights/alexnet_weights.h5", heatmap=False)
model.compile(optimizer=sgd, loss='mse')
print "Model Compiled"
train = model.predict(x_train)
print "Predictions made"
np.savez("../USNS/AlexNet/train/Data.npz",X_train=train)
Exemplo n.º 13
0
        os.chdir("/mnt/das4-fs4/")  # when executed locally
    except OSError as e:
        os.chdir("/")  # when executed on das-4
    os.chdir("var/scratch/rdchiaro/")

    # TODO: NOT WORKING WITH VGG-16 !! :(
    # predict('car.jpg', layer_dim=[], weights='weights/vgg16_weights.h5')
    #

    # TODO: NOT WORKING WITH VGG-16 !! :(
    s = "n02084071"
    ids = synset_to_dfs_ids(s)
    # Most of the synsets are not in the subset of the synsets used in ImageNet recognition task.
    ids = np.array([id_ for id_ in ids if id_ is not None])

    im = preprocess_image_batch(['dog.jpg'], color_mode="rgb")

    # Test pretrained model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('alexnet',
                    weights_path="weights/alexnet_weights.h5",
                    heatmap=True)
    model.compile(optimizer=sgd, loss='mse')

    out = model.predict(im)
    s = "n02084071"
    ids = synset_to_dfs_ids(s)
    print ids
    heatmap = out[0, ids].sum(axis=0)
    print heatmap
    # Then, we can get the image
Exemplo n.º 14
0
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/index.jpg'],
                            img_size=(256, 256),
                            crop_size=(227, 227),
                            color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',
                weights_path="weights/alexnet_weights(1).h5",
                heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)
Exemplo n.º 15
0
# Load an image (Use more later)
categories = [
    "table", "car", "airplane", "church", "fruit", "house", "dog", "cat",
    "teapot", "table lamp", "castle", "pillow", "volcano", "coffee mug",
    "envelope"
]
categoryPaths = [[
    'data/' + category + '/0000000' + str(i) + '.jpg' for i in range(500)
] for category in categories]

outputs = []
for category, paths in enumerate(categoryPaths):
    print('start processing category ' + categories[category])
    # And resize it to fit
    imgs = preprocess_image_batch(paths,
                                  img_size=(256, 256),
                                  crop_size=(224, 224),
                                  color_mode="bgr")
    # Specify Model Parameters, load pretrained weights and compile Model
    sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
    model = convnet('vgg_16', weights_path="vgg16_weights.h5", heatmap=False)
    model.compile(optimizer=sgd, loss='mse')

    # Predict image
    out = model.predict(imgs)
    print('saving')
    json.dump({categories[category]: out.tolist()},
              open(categories[category] + '.json', 'w'))
    outputs.append({categories[category]: out.tolist()})

print('save final result')
json.dump(outputs, open('classifications.json', 'w'))
Exemplo n.º 16
0

# model.load_weights("./weights/alexnet_weights.h5")

# im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

# model = convnet('alexnet',weights_path="./weights/alexnet_weights.h5", heatmap=False, trainable=False)

model = VGG_16(
    "/home/deep_learning_seminar/dlcv04/task4/weights/vgg16_weights.h5")
# model = convnet('VGG_16',weights_path="./weights/vgg16_weights.h5", heatmap=False)

# model.layers.pop()

# for layer in model.layers:
#     layer.trainable=False

# layer_last=Dense(1000, activation='softmax')
# layer_last.trainable=True

# model.add(layer_last)

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='mse', metrics=['accuracy'])
out = model.predict_proba(
    preprocess_image_batch([
        '/home/deep_learning_seminar/dlcv04/utils/alexnet/examples/kitten_crop_face.jpg'
    ],
                           img_size=(224, 224)))
print np.argmax(out)
print out
Exemplo n.º 17
0
    if weights_path:
        model.load_weights(weights_path)

    return model


# model.load_weights("./weights/alexnet_weights.h5")

# im = preprocess_image_batch(['examples/dog.jpg'],img_size=(256,256), crop_size=(227,227), color_mode="rgb")

# model = convnet('alexnet',weights_path="./weights/alexnet_weights.h5", heatmap=False, trainable=False)

model=VGG_16("/home/deep_learning_seminar/dlcv04/task4/weights/vgg16_weights.h5")
# model = convnet('VGG_16',weights_path="./weights/vgg16_weights.h5", heatmap=False)

# model.layers.pop() 

# for layer in model.layers:
#     layer.trainable=False 

# layer_last=Dense(1000, activation='softmax')
# layer_last.trainable=True

# model.add(layer_last)


sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='mse', metrics=['accuracy'])
out = model.predict_proba(preprocess_image_batch(['/home/deep_learning_seminar/dlcv04/utils/alexnet/examples/kitten_crop_face.jpg'],img_size=(224,224)))
print np.argmax(out)
print out
Exemplo n.º 18
0
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
from convnetskeras.imagenet_tool import synset_to_dfs_ids

#im = preprocess_image_batch(['examples/bern.jpg'], color_mode="bgr")
im = preprocess_image_batch(['examples/test.png'], color_mode="bgr")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet(
    'alexnet',
    weights_path="../../../../models/fully_conv/alexnet_weights.h5",
    heatmap=True)
#model = convnet('vgg_16',weights_path="../../../../models/fully_conv/vgg16_weights.h5", heatmap=True)

model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

s = "n02084071"
ids = synset_to_dfs_ids(s)
heatmap = out[0, ids].sum(axis=0)

# Then, we can get the image
import matplotlib.pyplot as plt
plt.imsave("examples/heatmap.png", heatmap)
Exemplo n.º 19
0
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet
import theano
import numpy as np

theano.config.exception_verbosity = 'high'
im = preprocess_image_batch([
    'examples/dog.jpg', 'examples/dog2.jpeg', 'examples/dog3.jpeg',
    'examples/dog4.jpeg', 'examples/dog5.jpeg', 'examples/dog6.jpeg',
    'examples/dog7.jpeg'
],
                            img_size=(256, 256),
                            crop_size=(227, 227),
                            color_mode="rgb")
print im.shape

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',
                weights_path="weights/alexnet_weights.h5",
                heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)
print out.shape
print np.argmax(out, axis=1)
Exemplo n.º 20
0
from keras.optimizers import SGD
from convnetskeras.convnets import preprocess_image_batch, convnet

im = preprocess_image_batch(['examples/' + img_name],
                            img_size=(256, 256),
                            crop_size=(227, 227),
                            color_mode="rgb")

sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)
model = convnet('alexnet',
                weights_path="weights/alexnet_weights.h5",
                heatmap=False)
model.compile(optimizer=sgd, loss='mse')

out = model.predict(im)

print out

best = -1
bestIndex = -1

for index, num in enumerate(out[0]):

    if (num > best):
        best = num
        bestIndex = index

print best
print bestIndex
for each in ['image_paths_live_validation', 'image_paths_spoof_validation']:
    dummy = []
    for each2 in eval(each):
        if each == 'image_paths_live_validation':
            targets_validation.append([1, 0])
        if each == 'image_paths_spoof_validation':
            targets_validation.append([0, 1])

#just_file_names = []
#for each in image_paths:
#    just_file_names.append(each.split('/')[8])
#targets = np.concatenate((np.ones((len(image_paths_live),3,227,227)),np.zeros((len(image_paths_spoof),3,227,227))))
#targets = np.array([list(np.concatenate((np.ones(len(image_paths_live)),np.zeros(len(image_paths_spoof))))).reshape(len(image_paths_live)+len(image_paths_spoof),1), list(np.concatenate((np.zeros(len(image_paths_live)),np.ones(len(image_paths_spoof))))).reshape(len(image_paths_live)+len(image_paths_spoof),1)])

im = preprocess_image_batch(im_full_paths,
                            img_size=(256, 256),
                            crop_size=(227, 227),
                            color_mode="rgb")
im_validation = preprocess_image_batch(im_full_paths_validation,
                                       img_size=(256, 256),
                                       crop_size=(227, 227),
                                       color_mode="rgb")

base_model = convnet(
    'alexnet',
    weights_path=
    "/Users/km4n6/Box Sync/kiran/NN_project/final_project/weights/alexnet_weights.h5",
    heatmap=False)
x = base_model.output
#x=Dropout(0.5)(x)
x = Dense(550,
          input_dim=500,