예제 #1
0
def get_image_feature(args):
    '''
	Extracts deep features from the prebuilt VGG-16 network.
	This is a function run by a subprocess.
 	[input]
	* i: index of training image
	* image_path: path of image file
	* vgg16: prebuilt VGG-16 network.
	* time_start: time stamp of start time
 	[saved]
	* feat: evaluated deep feature
	'''

    if DEBUGGING:
        i, image_path, vgg16 = args
        im = imageio.imread(image_path)
        im = preprocess_image(im)
        im = torch.from_numpy(im).unsqueeze(0)
        print(i, "start")
        fc7_out = vgg16(im)
        feature = fc7_out.detach().numpy()[-1]
        print(i, "finish", feature.shape)
        return feature
    else:
        i, image_path, vgg16_weights = args
        im = imageio.imread(image_path)
        im = preprocess_image(im)
        print(i, "start")
        feature = network_layers.extract_deep_feature(im, vgg16_weights)
        print(i, "finish", feature.shape)
        return feature
예제 #2
0
def get_image_feature(args):
    '''
	Extracts deep features from the prebuilt VGG-16 network.
	This is a function run by a subprocess.
 	[input]
	* i: index of training image
	* image_path: path of image file
	* vgg16: prebuilt VGG-16 network.
	* time_start: time stamp of start time
	[saved]
	* feat: evaluated deep feature
	'''

    # i, image_path, vgg16 = args
    # image = skimage.io.imread("../data/" + image_path)
    # image = preprocess_image(image)
    #
    # linear1 = torch.nn.Sequential(*list(vgg16.children())[0])
    # adaptivepool = list(vgg16.children())[1]
    # linear2 = torch.nn.Sequential(*list(vgg16.children())[2][:4])
    #
    # feat = linear2(adaptivepool(linear1(image)).flatten())
    # feat = feat.detach().numpy().reshape(1, 4096)
    # np.save('../results/feat.npy', feat)
    #
    # If we use our self-built model, use the below code
    i, image_path, vgg16 = args
    image = skimage.io.imread("../data/" + image_path)
    feat = network_layers.extract_deep_feature(image, vgg16)
    np.save('../results/feat.npy', feat)

    return feat
def process(train_files,label,vgg16_weights,i):

	feature=np.zeros((1001))
	path = os.path.join('../data/',train_files[0])
	image = imageio.imread(path).astype('double')
	image=skimage.transform.resize(image,(224,224,3))

	feature[:1000]=network_layers.extract_deep_feature(image,vgg16_weights)
	feature[1000]=label
	#add label to the end of the feature
	feature_label=np.asarray(feature)
	np.save('../deep_features/{}.npy'.format(i), feature_label)
	print('get deep features:',i,'shape:',feature_label.shape)
	return feature
예제 #4
0
def evaluate_one_image(args):

	train_features = np.load("trained_system_deep.npz")['features']
	labels = np.load("trained_system_deep.npz")['labels']

	i, image_path = args
	img_path = "../data/"+image_path[0]
	image = imageio.imread(img_path)
	img = preprocess_image(image)

	weights = util.get_VGG16_weights()
	feature = network_layers.extract_deep_feature(img, weights)
	label = labels[np.argmax(distance_to_set(feature, train_features))]
	print("Evaluation done for image ", i)
	np.save("../temp/"+"predicted_label_deep_"+str(i)+".npy", label)
예제 #5
0
def predict_image(args):
    '''
    Predicts the label using the trained system and extracted VGG-16 features
    This is a function run by a subprocess.

    [input]
    * image_path: path of image file
    * features: trained features from VGG-16
    * train_labels: trained set of labels 
    * vgg16: prebuilt VGG-16 network
    
    [output]
    * predicted_label: int representing the predicted label
    '''

    global PROGRESS
    with PROGRESS_LOCK:
        PROGRESS += NPROC

    image_path, features, train_labels, vgg16 = args
    print('Processing: %03d/160 | Image: %s' % (PROGRESS, image_path))

    # Read and preprocess image
    image = imageio.imread('../data/' + image_path)
    image = preprocess_image(image)

    # Extract deep features
    if USE_PYTORCH:
        feat = vgg16.features(image).flatten()  # VGG-16 Features
        feat = vgg16.classifier[:5](
            feat).detach().numpy().flatten()  # VGG-16 Classifiers
    else:
        feat = network_layers.extract_deep_feature(image,
                                                   util.get_VGG16_weights())

    # Find the predicted label
    predicted_label = train_labels[np.argmin(distance_to_set(feat, features))]
    return predicted_label
예제 #6
0
def get_image_feature(args):
    '''
    Extracts deep features from the prebuilt VGG-16 network
    This is a function run by a subprocess

    [input]
    * i: index of training image
    * image_path: path of image file
    * vgg16: prebuilt VGG-16 network
    
    [saved]
    * feat: evaluated deep feature
    '''

    global PROGRESS
    with PROGRESS_LOCK:
        PROGRESS += NPROC

    i, image_path, vgg16 = args
    print('Processing: %04d/1440 | Index: %04d | Image: %s' %
          (PROGRESS, i, image_path))

    # Read and preprocess image
    image = imageio.imread('../data/' + image_path)
    image = preprocess_image(image)

    # Extract deep features
    if USE_PYTORCH:
        feat = vgg16.features(image).flatten()  # VGG-16 Features
        feat = vgg16.classifier[:5](
            feat).detach().numpy().flatten()  # VGG-16 Classifiers
    else:
        feat = network_layers.extract_deep_feature(image,
                                                   util.get_VGG16_weights())

    # Save the extracted features
    np.save('%s/%d' % (TEMP_PATH, i), feat)
    return feat
예제 #7
0
def get_image_feature(args):
	'''
	Extracts deep features from the prebuilt VGG-16 network.
	This is a function run by a subprocess.
 	[input]
	* i: index of training image
	* image_path: path of image file
	* vgg16: prebuilt VGG-16 network.
	* time_start: time stamp of start time
 	[saved]
	* feat: evaluated deep feature
	'''

	i, image_path = args

	img_path = "../data/"+image_path[0]
	image = imageio.imread(img_path)
	img = preprocess_image(image)

	weights = util.get_VGG16_weights()
	feature = network_layers.extract_deep_feature(img, weights)

	np.save("../temp/"+"image_feature_"+str(i)+".npy", feature)