def create_train_data(self):
        # 将增强之后的训练集生成npy         

        print('-' * 30)
        print('creating train image')
        print('-' * 30)
        trainPath=self.train_path
        labelPath=self.label_path
        imgs = glob.glob(trainPath + '/*' + '.jpg')
        imgs=sorted(imgs)
        labels = glob.glob(labelPath + '/*' + '.jpg')
        labels=sorted(labels)
        
        imgdatas = np.ndarray((len(imgs), self.out_rows, self.out_cols, 1), dtype=np.uint8)
        imglabels = np.ndarray((len(labels), self.out_rows, self.out_cols, 1), dtype=np.uint8)
        for i in range(len(imgs)):
            
            #img = plt.imread(imgs[i])
            #label = plt.imread(labels[i])
            img = load_img(imgs[i], grayscale=True)
            label = load_img(labels[i], grayscale=True) 
            img = img_to_array(img)
            label = img_to_array(label)
            imgdatas[i] = img
            imglabels[i] = label
                #if i % 100 == 0:
                    #print('Done: {0}/{1} images'.format(i, len(imgs)))
            print(i)
        print('loading done', imgdatas.shape)
        np.save(self.npy_path + '/imgs_train.npy', imgdatas)            # 将30张训练集和30张label生成npy数据
        np.save(self.npy_path + '/imgs_mask_train.npy', imglabels)
        print('Saving to .npy files done.')
Esempio n. 2
0
	def create_train_data(self):
		i = 0
		print('-'*30)
		print('Creating training images...')
		print('-'*30)
		imgs = glob.glob(self.data_path+"/*."+self.img_type)
		print(len(imgs))
		imgdatas = np.ndarray((len(imgs),self.out_rows,self.out_cols,1), dtype=np.uint8)
		imglabels = np.ndarray((len(imgs),self.out_rows,self.out_cols,1), dtype=np.uint8)
		for imgname in imgs:
			midname = imgname[imgname.rindex("/")+1:]
			img = load_img(self.data_path + "/" + midname,grayscale = True)
			label = load_img(self.label_path + "/" + midname,grayscale = True)
			img = img_to_array(img)
			label = img_to_array(label)
			#img = cv2.imread(self.data_path + "/" + midname,cv2.IMREAD_GRAYSCALE)
			#label = cv2.imread(self.label_path + "/" + midname,cv2.IMREAD_GRAYSCALE)
			#img = np.array([img])
			#label = np.array([label])
			imgdatas[i] = img
			imglabels[i] = label
			if i % 100 == 0:
				print('Done: {0}/{1} images'.format(i, len(imgs)))
			i += 1
		print('loading done')
		np.save(self.npy_path + '/imgs_train.npy', imgdatas)
		np.save(self.npy_path + '/imgs_mask_train.npy', imglabels)
		print('Saving to .npy files done.')
    def create_test_data(self):
        # 测试集生成npy
        print('-' * 30)
        print('Creating test images...')
        print('-' * 30)
        imgs1 = glob.glob(self.test_path + "/*." + self.img_type)           # deform/train
        imgs1=sorted(imgs1)
        labels1 = glob.glob(self.test_label_path + '/*' + '.jpg')
        labels1=sorted(labels1)
        
        
        imgdatas1 = np.ndarray((len(imgs1), self.out_rows, self.out_cols, 1), dtype=np.uint8)
        imglabels1 = np.ndarray((len(labels1), self.out_rows, self.out_cols, 1), dtype=np.uint8)
        
        for i in range(len(labels1)):
            #img = plt.imread(imgs[i])
            #label = plt.imread(labels[i])
            
            label = load_img(labels1[i], grayscale=True)
            img = load_img(imgs1[i], grayscale=True)
            img = img_to_array(img)
            label = img_to_array(label)
            imgdatas1[i] = img
            imglabels1[i] = label
            print(i)

        print('loading done', imgdatas1.shape)
        np.save(self.npy_path + '/imgs_test.npy', imgdatas1)            # 将30张训练集和30张label生成npy数据
        np.save(self.npy_path + '/imgs_mask_test.npy', imglabels1)
        print('Saving to .npy files done.')
Esempio n. 4
0
def load_mask_labels():
    '''Load both target and style masks.
    A mask image (nr x nc) with m labels/colors will be loaded
    as a 4D boolean tensor: (1, m, nr, nc) for 'th' or (1, nr, nc, m) for 'tf'
    '''
    target_mask_img = load_img(target_mask_path,
                               target_size=(img_nrows, img_ncols))
    target_mask_img = img_to_array(target_mask_img)
    style_mask_img = load_img(style_mask_path,
                              target_size=(img_nrows, img_ncols))
    style_mask_img = img_to_array(style_mask_img)
    if K.image_dim_ordering() == 'th':
        mask_vecs = np.vstack([style_mask_img.reshape((3, -1)).T,
                               target_mask_img.reshape((3, -1)).T])
    else:
        mask_vecs = np.vstack([style_mask_img.reshape((-1, 3)),
                               target_mask_img.reshape((-1, 3))])

    labels = kmeans(mask_vecs, nb_labels)
    style_mask_label = labels[:img_nrows *
                              img_ncols].reshape((img_nrows, img_ncols))
    target_mask_label = labels[img_nrows *
                               img_ncols:].reshape((img_nrows, img_ncols))

    stack_axis = 0 if K.image_dim_ordering() == 'th' else -1
    style_mask = np.stack([style_mask_label == r for r in xrange(nb_labels)],
                          axis=stack_axis)
    target_mask = np.stack([target_mask_label == r for r in xrange(nb_labels)],
                           axis=stack_axis)

    return (np.expand_dims(style_mask, axis=0),
            np.expand_dims(target_mask, axis=0))
Esempio n. 5
0
	def Augmentation(self):

		"""
		Start augmentation.....
		"""
		trains = self.train_imgs
		labels = self.label_imgs
		path_train = self.train_path
		path_label = self.label_path
		path_merge = self.merge_path
		imgtype = self.img_type
		path_aug_merge = self.aug_merge_path
		if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
			print "trains can't match labels"
			return 0
		for i in range(len(trains)):
			img_t = load_img(path_train+"/"+str(i)+"."+imgtype)
			img_l = load_img(path_label+"/"+str(i)+"."+imgtype)
			x_t = img_to_array(img_t)
			x_l = img_to_array(img_l)
			x_t[:,:,2] = x_l[:,:,0]
			img_tmp = array_to_img(x_t)
			img_tmp.save(path_merge+"/"+str(i)+"."+imgtype)
			img = x_t
			img = img.reshape((1,) + img.shape)
			savedir = path_aug_merge + "/" + str(i)
			if not os.path.lexists(savedir):
				os.mkdir(savedir)
			self.doAugmentate(img, savedir, str(i))
Esempio n. 6
0
	def create_train_data(self):
		# 将增强之后的训练集生成npy
		i = 0
		print('-' * 30)
		print('creating train image')
		print('-' * 30)
		count = 0
		for indir in os.listdir(self.aug_merge_path):
			path = os.path.join(self.aug_merge_path, indir)
			count += len(os.listdir(path))
		imgdatas = np.ndarray((count, self.out_rows, self.out_cols, 1), dtype=np.uint8)
		imglabels = np.ndarray((count, self.out_rows, self.out_cols, 1), dtype=np.uint8)
		for indir in os.listdir(self.aug_merge_path):
			trainPath = os.path.join(self.aug_train_path, indir)
			labelPath = os.path.join(self.aug_label_path, indir)
			print(trainPath, labelPath)
			imgs = glob.glob(trainPath + '/*' + '.tif')
			for imgname in imgs:
				trainmidname = imgname[imgname.rindex('/') + 1:]
				labelimgname = imgname[imgname.rindex('/') + 1:imgname.rindex('_')] + '_label.tif'
				print(trainmidname, labelimgname)
				img = load_img(trainPath + '/' + trainmidname, grayscale=True)
				label = load_img(labelPath + '/' + labelimgname, grayscale=True)
				img = img_to_array(img)
				label = img_to_array(label)
				imgdatas[i] = img
				imglabels[i] = label
				if i % 100 == 0:
					print('Done: {0}/{1} images'.format(i, len(imgs)))
				i += 1
				print(i)
		print('loading done', imgdatas.shape)
		np.save(self.npy_path + '/imgs_train.npy', imgdatas)            # 将30张训练集和30张label生成npy数据
		np.save(self.npy_path + '/imgs_mask_train.npy', imglabels)
		print('Saving to .npy files done.')
Esempio n. 7
0
	def create_small_train_data(self):
		# 将增强之后的训练集生成npy
		print('-' * 30)
		print('creating samll train image')
		print('-' * 30)
		imgs = glob.glob('../data_set/aug_train/0/*' + '.tif')
		count = len(imgs)
		imgdatas = np.ndarray((count, self.out_rows, self.out_cols, 1), dtype=np.uint8)
		imglabels = np.ndarray((count, self.out_rows, self.out_cols, 1), dtype=np.uint8)
		trainPath = '../data_set/aug_train/0'
		labelPath = '../data_set/aug_label/0'
		i = 0
		for imgname in imgs:
			trainmidname = imgname[imgname.rindex('/') + 1:]
			labelimgname = imgname[imgname.rindex('/') + 1:imgname.rindex('_')] + '_label.tif'
			print(trainmidname, labelimgname)
			img = load_img(trainPath + '/' + trainmidname, grayscale=True)
			label = load_img(labelPath + '/' + labelimgname, grayscale=True)
			img = img_to_array(img)
			label = img_to_array(label)
			imgdatas[i] = img
			imglabels[i] = label
			i += 1
			print(i)
		print('loading done', imgdatas.shape)
		np.save(self.npy_path + '/imgs_small_train.npy', imgdatas)  # 将30张训练集和30张label生成npy数据
		np.save(self.npy_path + '/imgs_mask_small_train.npy', imglabels)
		print('Saving to .npy files done.')
Esempio n. 8
0
	def augmentation(self):
		# 读入3通道的train和label, 分别转换成矩阵, 然后将label的第一个通道放在train的第2个通处, 做数据增强
		print("运行 Augmentation")

		# Start augmentation.....
		trains = self.train_imgs
		labels = self.label_imgs
		path_train = self.train_path
		path_label = self.label_path
		path_merge = self.merge_path
		imgtype = self.img_type
		path_aug_merge = self.aug_merge_path
		print(len(trains), len(labels))
		if len(trains) != len(labels) or len(trains) == 0 or len(trains) == 0:
			print("trains can't match labels")
			return 0
		for i in range(len(trains)):
			img_t = load_img(path_train + "/" + str(i) + "." + imgtype)  # 读入train
			img_l = load_img(path_label + "/" + str(i) + "." + imgtype)  # 读入label
			x_t = img_to_array(img_t)                                    # 转换成矩阵
			x_l = img_to_array(img_l)
			x_t[:, :, 2] = x_l[:, :, 0]                                  # 把label当做train的第三个通道
			img_tmp = array_to_img(x_t)
			img_tmp.save(path_merge + "/" + str(i) + "." + imgtype)      # 保存合并后的图像
			img = x_t
			img = img.reshape((1,) + img.shape)                          # 改变shape(1, 512, 512, 3)
			savedir = path_aug_merge + "/" + str(i)                      # 存储合并增强后的图像
			if not os.path.lexists(savedir):
				os.mkdir(savedir)
			self.do_augmentate(img, savedir, str(i))                      # 数据增强
Esempio n. 9
0
def load_cat_dog(HEIGHT, WIDTH, train_frac, test_frac):
    img_dir = './Datasets/cat_dog/train/'
    files = os.listdir(img_dir)

    N_CATEGORY = 2
    N_ALL = len(files)

    N_TRAIN = int(train_frac * N_ALL)
    x_train = np.empty((N_TRAIN, HEIGHT, WIDTH, 3), np.uint8)
    y_train = np.empty((N_TRAIN,), np.int8)

    for i in range(N_TRAIN):
        filename = files[i]
        img = load_img(img_dir + filename)
        img = img.resize((HEIGHT,WIDTH))
        x_train[i,] = img

        entry = filename.split('.')
        y_train[i] = 1 if entry[0] == 'dog' else 0

    N_TEST = int(test_frac * N_ALL)
    x_test = np.empty((N_TEST, HEIGHT, WIDTH, 3), np.uint8)
    y_test = np.empty((N_TEST,), np.int8)

    for i in range(N_TEST):
        filename = files[i + N_TRAIN]
        img = load_img(img_dir + filename)
        img = img.resize((HEIGHT,WIDTH))
        x_test[i,] = img

        entry = filename.split('.')
        y_test[i] = 1 if entry[0] == 'dog' else 0

    return x_train, y_train, x_test, y_test, N_CATEGORY
Esempio n. 10
0
    def test_load_img(self, tmpdir):
        filename = str(tmpdir / 'image.png')

        original_im_array = np.array(255 * np.random.rand(100, 100, 3),
                                     dtype=np.uint8)
        original_im = image.array_to_img(original_im_array, scale=False)
        original_im.save(filename)

        # Test that loaded image is exactly equal to original.

        loaded_im = image.load_img(filename)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename, grayscale=True)
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test that nothing is changed when target size is equal to original.

        loaded_im = image.load_img(filename, target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == original_im_array.shape
        assert np.all(loaded_im_array == original_im_array)

        loaded_im = image.load_img(filename, grayscale=True,
                                   target_size=(100, 100))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (original_im_array.shape[0],
                                         original_im_array.shape[1], 1)

        # Test down-sampling with bilinear interpolation.

        loaded_im = image.load_img(filename, target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 3)

        loaded_im = image.load_img(filename, grayscale=True,
                                   target_size=(25, 25))
        loaded_im_array = image.img_to_array(loaded_im)
        assert loaded_im_array.shape == (25, 25, 1)

        # Test down-sampling with nearest neighbor interpolation.

        loaded_im_nearest = image.load_img(filename, target_size=(25, 25),
                                           interpolation="nearest")
        loaded_im_array_nearest = image.img_to_array(loaded_im_nearest)
        assert loaded_im_array_nearest.shape == (25, 25, 3)
        assert np.any(loaded_im_array_nearest != loaded_im_array)

        # Check that exception is raised if interpolation not supported.

        loaded_im = image.load_img(filename, interpolation="unsupported")
        with pytest.raises(ValueError):
            loaded_im = image.load_img(filename, target_size=(25, 25),
                                       interpolation="unsupported")
Esempio n. 11
0
def path_to_tensor(img_path):
    # 用PIL加载RGB图像为PIL.Image.Image类型
    img = image.load_img(img_path, target_size=(224, 224))
    # 将PIL.Image.Image类型转化为格式为(224, 224, 3)的3维张量
    x = image.img_to_array(img)
    # 将3维张量转化为格式为(1, 224, 224, 3)的4维张量并返回
    return np.expand_dims(x, axis=0)
Esempio n. 12
0
 def _get_batches_of_transformed_samples(self, index_array):
     batch_x = np.zeros((len(index_array),) + self.image_shape, dtype=K.floatx())
     if self.with_labels:
         batch_y = np.zeros((len(batch_x), self.num_class), dtype=K.floatx())
     for i, j in enumerate(index_array):
         # Protect file and dataframe access with a lock.
         with self.lock:
             image_row = self.images_df.iloc[j]
             product_id = image_row["product_id"]
             offset_row = self.offsets_df.loc[product_id]
             # Read this product's data from the BSON file.
             self.file.seek(offset_row["offset"])
             item_data = self.file.read(offset_row["length"])
         # Grab the image from the product.
         item = bson.BSON.decode(item_data)
         img_idx = image_row["img_idx"]
         bson_img = item["imgs"][img_idx]["picture"]
         # Load the image.
         img = load_img(io.BytesIO(bson_img), target_size=self.target_size)
         # Preprocess the image.
         x = img_to_array(img)
         x = preprocess_image(x)
         #x = self.image_data_generator.random_transform(x)
         #x = self.image_data_generator.standardize(x)
         # Add the image and the label to the batch (one-hot encoded).
         batch_x[i] = x
         if self.with_labels:
             batch_y[i, image_row["category_idx"]] = 1
     if self.with_labels:
         return batch_x, batch_y
     else:
         return batch_x
Esempio n. 13
0
def run():
    parser = argparse.ArgumentParser()
    parser.add_argument("--file", help="file to process")
    parser.add_argument("--zoom", help="repeat zoom", type=int, default=1)
    args = parser.parse_args()

    if not args.file:
        return train_on_image_net()

    logger.debug('Start processing')
    logger.debug('Loading image')

    img = load_img(args.file)

    logger.debug('Opening zoom model')
    model = get_model_zoom(FEATURE_SIZE, IMAGE_SIZE, CONV_SIZE, CHANNELS)
    logger.debug('Loading zoom model')
    model.load_weights('weights/' + MODEL_NAME)

    logger.debug('Resizing image with standard filter.')
    img = img.resize((int(img.width * ZOOM_LEARN), int(img.height * ZOOM_LEARN)), ZOOM_TYPE)
    logger.debug('Zooming.')
    img = predict_base_image(CHANNELS, img, model, MODE)

    img.save('result.png', 'PNG')
    img.show()
Esempio n. 14
0
def merge_image(original_image_path, depth_image_path, width, height):

    d_img = load_img(depth_image_path, grayscale=True)
    d_array = img_to_array(d_img).reshape(height, width)
    print(d_array.shape)

    o_img = load_img(original_image_path, grayscale=False)
    o_array = img_to_array(o_img)
    print(o_array.shape)


    combined_array = np.zeros((height, width, 3), dtype="float32")
    combined_array[:, :, 0:3] = o_array # The first three layers will be the original one
    combined_array[:, :, 3] = d_array

    return combined_array
Esempio n. 15
0
def loadImages():

	# get current working directory path
	folder = 'data'  # bats dataset

	dataroot = os.getcwd() + '/' + folder
	directoryList = os.listdir(dataroot)

	imageList =[]

	# loop over all the subdirectories and create the image array of all the images and store it in an array
	for categoryDir in directoryList:
		currentImageList = os.listdir(dataroot +'/'+ categoryDir)
		print ('Loading images for '+'{}\n'.format(categoryDir))
		for currentImage in currentImageList:
			imagePath = dataroot + '/'+ categoryDir + '/'+ currentImage 
			currentImage = image.load_img(imagePath, target_size=(224, 224))
			x = image.img_to_array(currentImage)
			x = np.expand_dims(x, axis=0)
			x = preprocess_input(x)
			print('Image shape:', x.shape)
			imageList.append(x)


	# reshaping the data as needed by the resnet50 model i.e. (<observations>, 224, 224, 3)
	imageData = np.array(imageList)
	print(imageData.shape)  # it should show (40,1, 224, 224, 3)

	imageData=np.rollaxis(imageData,1,0)
	print (imageData.shape) # it should show (1, 40, 224, 224, 3) 

	imageData=imageData[0]
	print (imageData.shape) # now it should show (40, 224, 224, 3) , which is exactly the shape that we need

	return imageData
Esempio n. 16
0
def load_images(random_state=1234):
    train_df = pd.read_csv("data/train.csv", index_col="id", usecols=[0])
    depths_df = pd.read_csv("data/depths.csv", index_col="id")
    train_df = train_df.join(depths_df)
    test_df = depths_df[~depths_df.index.isin(train_df.index)]
    print(">>> train_df:",train_df.shape)
    print(train_df.head())
    print(">>> test_df:", test_df.shape)
    print(test_df.head())
    train_df["images"] = [gradmag(np.array(imread(path_train_images+"{}.png".format(idx)))) for idx in tqdm(train_df.index)]
    train_df["masks"] = [np.array(load_img(path_train_masks+"{}.png".format(idx),grayscale=True))/255 for idx in tqdm(train_df.index)]
    train_df["coverage"] = train_df.masks.map(np.sum) / pow(img_size_ori, 2)
    train_df["coverage_class"] = train_df.coverage.map(cov_to_class)
    print("*** TRAIN ***")
    print(train_df.head())
    print("*** TEST ***")
    print(test_df.head())
    ids_train, ids_valid, x_train, x_valid, y_train, y_valid, cov_train, cov_test, depth_train, depth_test = train_test_split(
        train_df.index.values,
        np.array(train_df.images.tolist()).reshape(-1, img_size_target, img_size_target, 1),
        np.array(train_df.masks.tolist()).reshape(-1, img_size_target, img_size_target, 1),
        train_df.coverage.values,
        train_df.z.values,
        test_size=0.2,
        stratify=train_df.coverage_class,
        random_state=random_state)
    #Data augmentation
    x_train2 = np.append(x_train, [np.fliplr(x) for x in x_train], axis=0)
    y_train2 = np.append(y_train, [np.fliplr(x) for x in y_train], axis=0)
    print(x_train2.shape)
    print(y_valid.shape)
    x_test = np.array([gradmag(np.array(imread(path_test_images+"{}.png".format(idx)))) for idx in tqdm(test_df.index)]).reshape(-1, img_size_target, img_size_target, 1)
    return x_train2, x_valid, y_train2, y_valid, x_test, test_df.index.values
Esempio n. 17
0
def load_image(path):
    img_path = sys.argv[1]
    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)
    return x
Esempio n. 18
0
def predict_labels(model):
    """writes test image labels and predictions to csv"""
    
    test_datagen = ImageDataGenerator(rescale=1./255)
    test_generator = test_datagen.flow_from_directory(
        test_data_dir,
        target_size=(img_height, img_width),
        batch_size=32,
        shuffle=False,
        class_mode=None)

    base_path = "../data/test/test/"

    with open("prediction.csv", "w") as f:
        p_writer = csv.writer(f, delimiter=',', lineterminator='\n')
        for _, _, imgs in os.walk(base_path):
            for im in imgs:
                pic_id = im.split(".")[0]
                img = load_img(base_path + im)
                img = imresize(img, size=(img_height, img_width))
                test_x = img_to_array(img).reshape(3, img_height, img_width)
                test_x = test_x.reshape((1,) + test_x.shape)
                test_generator = test_datagen.flow(test_x,
                                                   batch_size=1,
                                                   shuffle=False)
                prediction = model.predict_generator(test_generator, 1)[0][0]
                p_writer.writerow([pic_id, prediction])
def augment_img(input_file, output_folder, img_format='jpg',
                number_imgs=10):
    """
    Generate number_imgs new images from a given image.
    This function is inspired from the following blog post:
    https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html
    """
    datagen = ImageDataGenerator(
        rotation_range=40,
        width_shift_range=0.2,
        height_shift_range=0.2,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True,
        fill_mode='nearest')

    img = load_img(input_file)
    x = img_to_array(img)
    x = x.reshape((1,) + x.shape)
    i = 0
    for batch in datagen.flow(x, batch_size=1,
                              save_to_dir=output_folder,
                              save_format=img_format):
        i += 1
        if i > number_imgs:
            break
Esempio n. 20
0
    def test_bare_keras_module(self):
        """ Keras GraphFunctions should give the same result as standard Keras models """
        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        for model_gen, preproc_fn in [(InceptionV3, iv3.preprocess_input),
                                      (Xception, xcpt.preprocess_input),
                                      (ResNet50, rsnt.preprocess_input)]:

            keras_model = model_gen(weights="imagenet")
            target_size = tuple(keras_model.input.shape.as_list()[1:-1])

            _preproc_img_list = []
            for fpath in img_fpaths:
                img = load_img(fpath, target_size=target_size)
                # WARNING: must apply expand dimensions first, or ResNet50 preprocessor fails
                img_arr = np.expand_dims(img_to_array(img), axis=0)
                _preproc_img_list.append(preproc_fn(img_arr))

            imgs_input = np.vstack(_preproc_img_list)

            preds_ref = keras_model.predict(imgs_input)

            gfn_bare_keras = GraphFunction.fromKeras(keras_model)

            with IsolatedSession(using_keras=True) as issn:
                K.set_learning_phase(0)
                feeds, fetches = issn.importGraphFunction(gfn_bare_keras)
                preds_tgt = issn.run(fetches[0], {feeds[0]: imgs_input})

            self.assertTrue(np.all(preds_tgt == preds_ref))
Esempio n. 21
0
    def test_pipeline(self):
        """ Pipeline should provide correct function composition """
        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        xcpt_model = Xception(weights="imagenet")
        stages = [('spimage', gfac.buildSpImageConverter(SparkMode.RGB_FLOAT32)),
                  ('xception', GraphFunction.fromKeras(xcpt_model))]
        piped_model = GraphFunction.fromList(stages)

        for fpath in img_fpaths:
            target_size = tuple(xcpt_model.input.shape.as_list()[1:-1])
            img = load_img(fpath, target_size=target_size)
            img_arr = np.expand_dims(img_to_array(img), axis=0)
            img_input = xcpt.preprocess_input(img_arr)
            preds_ref = xcpt_model.predict(img_input)

            spimg_input_dict = imageArrayToStruct(img_input).asDict()
            spimg_input_dict['data'] = bytes(spimg_input_dict['data'])
            with IsolatedSession() as issn:
                # Need blank import scope name so that spimg fields match the input names
                feeds, fetches = issn.importGraphFunction(piped_model, prefix="")
                feed_dict = dict((tnsr, spimg_input_dict[tfx.op_name(issn.graph, tnsr)]) for tnsr in feeds)
                preds_tgt = issn.run(fetches[0], feed_dict=feed_dict)
                # Uncomment the line below to see the graph
                # tfx.write_visualization_html(issn.graph,
                #                              NamedTemporaryFile(prefix="gdef", suffix=".html").name)

            self.assertTrue(np.all(preds_tgt == preds_ref))
Esempio n. 22
0
def convert_image_to_tensors(img_path):
    # loads RGB image as PIL.Image.Image type
    img = image.load_img(img_path, target_size=(224, 224))
    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    return np.expand_dims(x, axis=0)
Esempio n. 23
0
def load_img(input_path, target_shape, grayscale=False, mean=None, std=None):
    img = image.load_img(input_path, target_size=target_shape,
                         grayscale=grayscale)
    img_arr = np.expand_dims(image.img_to_array(img), axis=0)
    if not grayscale:
        img_arr = preprocess_input(img_arr, mean=mean, std=std)
    return img_arr
Esempio n. 24
0
    def test_spimage_converter_module(self):
        """ spimage converter module must preserve original image """
        img_fpaths = glob(os.path.join(_getSampleJPEGDir(), '*.jpg'))

        def exec_gfn_spimg_decode(spimg_dict, img_dtype):
            gfn = gfac.buildSpImageConverter(img_dtype)
            with IsolatedSession() as issn:
                feeds, fetches = issn.importGraphFunction(gfn, prefix="")
                feed_dict = dict((tnsr, spimg_dict[tfx.op_name(issn.graph, tnsr)]) for tnsr in feeds)
                img_out = issn.run(fetches[0], feed_dict=feed_dict)
            return img_out

        def check_image_round_trip(img_arr):
            spimg_dict = imageArrayToStruct(img_arr).asDict()
            spimg_dict['data'] = bytes(spimg_dict['data'])
            img_arr_out = exec_gfn_spimg_decode(spimg_dict, spimg_dict['mode'])
            self.assertTrue(np.all(img_arr_out == img_arr))

        for fp in img_fpaths:
            img = load_img(fp)

            img_arr_byte = img_to_array(img).astype(np.uint8)
            check_image_round_trip(img_arr_byte)

            img_arr_float = img_to_array(img).astype(np.float)
            check_image_round_trip(img_arr_float)

            img_arr_preproc = iv3.preprocess_input(img_to_array(img))
            check_image_round_trip(img_arr_preproc)
Esempio n. 25
0
def start():
    model = keras.applications.vgg19.VGG19(include_top=False, weights='imagenet', pooling='avg')

    root_dir = "/media/rishabh/dump_bin/Animals_with_Attributes2/JPEGImages/"
    for root, subdirs, files in os.walk(root_dir):
        list_file_path = os.path.join(root, 'list_of_files.txt')
        with open(list_file_path, 'wb') as list_file:

            for filename in files:
                if filename.endswith("jpg"):
                    file_path = os.path.join(root, filename)
                    img = image.load_img(file_path, target_size=(224,224))
                    x = image.img_to_array(img)
                    x = np.expand_dims(x, axis=0)
                    x = preprocess_input(x)

                    features = model.predict(x)

                    np_name = filename[0:-4]
                    np_name = np_name+".npy"
                    np.save(os.path.join(root,np_name), features)

        #            npy = open(os.path.join(root,np_name),"w+")
                    print('file %s (full path: %s)' % (filename, file_path))
                    list_file.write(('%s\n' % filename).encode('utf-8'))
Esempio n. 26
0
 def on_epoch_end(self, epoch, logs={}):
     filepath = self.filepath.format(epoch=epoch, **logs)
     if self.save_best_only:
         current = logs.get(self.monitor)
         if current is None:
             warnings.warn("Can save best model only with %s available, skipping." % (self.monitor), RuntimeWarning)
         else:
             if current < self.best:
                 if self.verbose > 0:
                     print("Epoch %05d: %s improved from %0.5f to %0.5f, saving model to %s"
                           % (epoch, self.monitor, self.best, current, filepath))
                 self.best = current
                 self.model.save_weights(filepath, overwrite=True)
                 self.model_dict['best'] = current
                 img = load_img('/home/robin/test-deblur-test.png')
                 img = predict_base_image_channels_1(1, img, self.model, 'RGB')
                 if self.viewer:
                     close(self.viewer)
                 self.viewer = show_img(img, self.no)
                 self.no += 1
                 # img.save('/home/robin/test-deblur.png', 'PNG')
             else:
                 if self.verbose > 0:
                     print("Epoch %05d: %s did not improve" % (epoch, self.monitor))
     else:
         if self.verbose > 0:
             print("Epoch %05d: saving model to %s" % (epoch, filepath))
         self.model.save_weights(filepath, overwrite=True)
Esempio n. 27
0
def val_images_to_array(img_path, source_path, img_dim, categories):

    array_path = os.path.join(source_path, 'array')
    shutil.rmtree(array_path,ignore_errors=True)
    os.makedirs(array_path)

    print('Iterating over all categories: ', categories)
    category_lengths = []
    for category_idx, category in enumerate(categories):
        print('categories:', category)
        category_path = os.path.join(img_path, category)
        img_files = sorted(os.listdir(category_path))
        category_lengths.append(len(img_files))
        for img_idx, img_file in tqdm(enumerate(img_files)):
            this_img_path = os.path.join(category_path, img_file)
            img = load_img(this_img_path, target_size=(img_dim, img_dim))

            img_name = '{}-img-{}-{}'.format(img_idx, category, category_idx)
            label_name = '{}-label-{}-{}'.format(img_idx, category, category_idx)

            label = np.eye(len(categories), dtype = np.float32)[category_idx]

            img_array_path = os.path.join(array_path, img_name)
            img_label_path = os.path.join(array_path, label_name)

            np.save(img_array_path, img)
            np.save(img_label_path, label)
    category_lengths = np.array(category_lengths) / sum(category_lengths)
    category_lengths = list(category_lengths / max(category_lengths))
    category_rounds = {cat: min(int(np.round(1 / l)), 10) for cat, l in zip(categories, category_lengths)}
    return category_rounds
def convex_hull(imagefile):
	im1=image.load_img(imagefile,target_size=(224,224))
	im1array=image.img_to_array(im1)
	hull=ConvexHull(im1array[0])
	print "Convex Hull vertices:",hull.vertices
	print "Convex Hull area:",hull.area
	return (hull.vertices, hull.area)
Esempio n. 29
0
def preprocess_xray_flipped(xray_path):
    xray = image.load_img(xray_path, color_mode="grayscale", vertical_flip=True,
                          target_size=(img_dims[0], img_dims[1], 1))
    xray = image.img_to_array(xray)
    xray = np.dstack([xray, xray, xray])
    xray = preprocess_input(xray)
    return xray
Esempio n. 30
0
def predict(image_file):
    img = image.load_img(image_file, target_size=(img_width,img_height))
    x = image.img_to_array(img)
    x = np.expand_dims(x,axis=0)
    x = preprocess_input(x)
    preds = model.predict(x)
    return preds
Esempio n. 31
0
def path_to_tensor(image_path, target_size):
    image = load_img(image_path, target_size=target_size)
    tensor = img_to_array(image)
    tensor = np.expand_dims(tensor, axis=0)
    return tensor
Esempio n. 32
0
else:
    print("cat")

classifier.save('my_model.h5')

"""

#After training
#Warning this code must only be added after model was creating. Luckily for you I've got a saved model in the .h5 so you can test off of that one
#However if you want to train another model, you'll have to comment out this below and remove the comments from the block above.
#The above code just creates the model though, the code below saves it's object data in a .h5 file and then also allows you to use the model
#-As many times as you want even after the program is killed. So no need to train it for hours again :D
from keras.models import load_model
classfier = load_model(
    '\hello.h5'
)  #please make sure the .h5 files is within the same dir, as the CNN.py file

import numpy as np
from keras.preprocessing import image
test_img = image.load_img(
    'F:\animal.png', target_size=(64, 64)
)  #please make sure you give the correct dir of where the image files is that you want to test
test_img = image.img_to_array(test_img)
test_img = np.expand_dims(test_img, axis=0)
result = classfier.predict(test_img)
#train_gen.class_indices
if result[0][0] > 0.5:
    print("dog")
else:
    print("cat")
Esempio n. 33
0
# Plot normalized confusion matrix
plt.figure()
plot_confusion_matrix(cm,
                      classes=class_names,
                      normalize=True,
                      title='Normalized confusion matrix')

plt.show()
# ------------------------------------------------------------------------------
# Make prediction for signle image
image_path = 'dataset/eva.2.png'
#orig = cv2.imread(image_path)

#print("[INFO] loading and preprocessing image...")
image = load_img(image_path, target_size=(in_s, in_s))
image = img_to_array(image)
# important! otherwise the predictions will be '0'
image = image / 255
image = np.expand_dims(image, axis=0)
# ------------------------------------------------------------------------------
# run the image through the same pipeline
# build the VGG16 network
model = applications.VGG16(include_top=False, weights='imagenet')

# get the bottleneck prediction from the pre-trained VGG16 model
bottleneck_prediction = model.predict(image)

# build top model
model = Sequential()
model.add(Flatten(input_shape=bottleneck_prediction.shape[1:]))
# -*- coding: utf-8 -*-
"""
Created on Fri Oct 18 22:37:37 2019

@author: SJana
"""

from keras.models import load_model
from keras.preprocessing.image import load_img, img_to_array
import numpy as np

model = load_model('document_classification_model.h5')

test_img = load_img('Documents/test_img/test/test1.jpg', target_size=(64, 64))
test_img.size
test_img.show()
test_image = img_to_array(test_img)
test_image = np.expand_dims(test_image, axis=0)
test_image.shape
image_label = model.predict(test_image)

print(image_label)
Esempio n. 35
0
def preprocess_image(img):
    img = image.load_img(img, target_size=(224, 224))
    img = image.img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    return img
# This is module with image preprocessing utilities
import os

import matplotlib.pyplot as plt
from cat_vs_dogs_create_data_sets import *
from keras.preprocessing import image

from cats_dogs.cats_vs_dgos_2 import datagen

fnames = [
    os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)
]
img_path = fnames[3]  # We pick one image to "augment"
img = image.load_img(img_path,
                     target_size=(150, 150))  # Read the image and resize it
x = image.img_to_array(
    img)  # Convert it to a Numpy array with shape (150, 150, 3)
x = x.reshape((1, ) + x.shape)  # Reshape it to (1, 150, 150, 3)
# The .flow() command below generates batches of randomly transformed images.
# It will loop indefinitely, so we need to `break` the loop at some point!
i = 0
for batch in datagen.flow(x, batch_size=1):
    plt.figure(i)
    imgplot = plt.imshow(image.array_to_img(batch[0]))
    i += 1
    if i % 4 == 0:
        break
plt.show()
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

### Learning TensorFlow with Laurence Moroney, Google Brain on Coursera
### /Users/trannguyen/TranData/WORK/BioinformaticsSpecialization_Tran_2019/\
###/MachineLearning/TensorFlow/TensorFlowCodes

import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():

    # predicting images
    path = '/content/' + fn
    img = image.load_img(path, target_size=(150, 150))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)

    images = np.vstack([x])
#  classes = model.predict(images, batch_size=10)
#  print(classes[0])
#  if classes[0]>0.5:
#    print(fn + " is a human")
#  else:
#    print(fn + " is a horse")
#
Esempio n. 38
0
    channels = 3
    size = img_h * img_w
    return K.sum(K.square(S - G) / (4 * (channels**2) * (size**2)))


def total_variation_loss(x):  # to smooth the generated image
    a = K.square(x[:, :img_h - 1, :img_w - 1, :] - x[:, 1:, :img_w - 1, :])
    b = K.square(x[:, :img_h - 1, :img_w - 1, :] - x[:, :img_h - 1, 1:, :])

    return K.sum(K.pow(a + b, 1.25))


CONTENT_IMG_PATH = "content.jpg"
STYLE_IMG_PATH = "style.jpg"

h, w = load_img(CONTENT_IMG_PATH).size
img_h = 400
img_w = int(h * img_h / w)

content_img = K.variable(preprocess(CONTENT_IMG_PATH))
style_img = K.variable(preprocess(STYLE_IMG_PATH))
gen_img = K.placeholder(shape=(1, img_h, img_w, 3))

input_tensor = K.concatenate([content_img, style_img, gen_img], axis=0)

model = vgg19.VGG19(include_top=False,
                    weights='imagenet',
                    input_tensor=input_tensor)
print('Model Loaded...')
print(model.summary())
Esempio n. 39
0
    pred_scores = np.zeros((5, 36))
    vgg_features = VGGFace(include_top=False, model='resnet50', input_shape=(224, 224, 3))

    gender = np.load('bounds/pggan_celebahq_gender_boundary.npy')
    age = np.load('bounds/pggan_celebahq_age_boundary.npy')
    eyeglasses = np.load('bounds/pggan_celebahq_eyeglasses_boundary.npy')
    pose = np.load('bounds/pggan_celebahq_pose_boundary.npy')
    smile = np.load('bounds/pggan_celebahq_smile_boundary.npy')
    boundaries = [gender, age, eyeglasses, pose, smile]

    Y_test = cupy.asnumpy(Y_test)
    T_test = cupy.asnumpy(T_test)
    for trial in range(trials):
        stim_ssim = np.array(Image.open("stimuli/%i.png" % trial))
        recon_ssim = np.array(Image.open("reconstructions/%i.png" % trial))
        stim_fsim = image.load_img("stimuli/%i.png" % trial, target_size=(224, 224))
        recon_fsim = image.load_img("reconstructions/%i.png" % trial, target_size=(224, 224))

        test_feats[trial] = get_features(stim_fsim)
        pred_feats[trial] = get_features(recon_fsim)
        metrics['lsim'][trial] = 1. / (1 + mean_squared_error(Y_test[trial], T_test[trial]))
        metrics['fsim'][trial] = 1. / (1 + mean_squared_error(test_feats[trial], pred_feats[trial]))
        metrics['ssim'][trial] = ssim(stim_ssim, recon_ssim, multichannel=True)

        for i, boundary in enumerate(boundaries):
            test_scores[i, trial] = T_test[trial].reshape(1, -1).dot(boundary.T)[0][0]
            pred_scores[i, trial] = Y_test[trial].reshape(1, -1).dot(boundary.T)[0][0]

    # print metrics
    print("latent similarity: %.4f" % metrics['lsim'].mean())
    print("Feature similarity: %.4f" % metrics['fsim'].mean())
def get_image(img_id):
    img = image.load_img(img_id, target_size=(input_size, input_size))
    img = image.img_to_array(img)
    img = img / 255.
    return img
Esempio n. 41
0
le = preprocessing.LabelEncoder()
le.fit(y_train)
y_train = le.transform(y_train) 
y_test = le.transform(y_test)
no_classes = len(np.unique(y_train))
print("Number of classes: " + str(no_classes))
y_train = to_categorical(y_train, num_classes = no_classes)
y_test = to_categorical(y_test, num_classes = no_classes)

images_train = np.zeros((x_train.shape[0],224,224,3))

print("Loading train images")
for i in range(x_train.shape[0]):
    image_name = path + str(x_train[i])
    img = image.img_to_array(image.load_img(image_name))
    images_train[i] = img

print(images_train.shape)
images_train = preprocess_input(images_train)


images_test = np.zeros((x_test.shape[0],224,224,3))

print("Loading test images")
for i in range(x_test.shape[0]):
    image_name = path + str(x_test[i])
    img = image.img_to_array(image.load_img(image_name))
    images_test[i] = img

print(images_test.shape)
# Importing necessary functions
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

# Initialising the ImageDataGenerator class.
# We will pass in the augmentation parameters in the constructor.
datagen = ImageDataGenerator(rotation_range=40,
                             shear_range=0.2,
                             zoom_range=0.2,
                             horizontal_flip=True,
                             brightness_range=(0.5, 1.5))
src = r'F:\bike_car\bike' + '\\'
ext = '.jpg'
for j in range(1, 619):
    # Loading a sample image
    img = load_img(src + str(j) + ext)
    # Converting the input sample image to an array
    x = img_to_array(img)
    # Reshaping the input image
    x = x.reshape((1, ) + x.shape)

    # Generating and saving 5 augmented samples
    # using the above defined parameters.
    i = 0
    for batch in datagen.flow(x,
                              batch_size=1,
                              save_to_dir='F:\\bike_car\\aug_bike',
                              save_prefix='image',
                              save_format='jpg'):
        i += 1
        if i > 5:
Esempio n. 43
0
def image_augment_for_directory(img_data_path='./img_data/',
                                text_data_path='./caption_text/',
                                save_img_path='./augment_img_data/',
                                save_text_path='./augment_caption_text/',
                                image_size=(224, 224),
                                augment_count=5):
    """
    对一个文件夹中所有的图像进行随机数据增强,并对其匹配对应的文本数据
    将增强后的数据+原始数据单独存在另一个文件夹,不要和原来的数据混合
    :param img_data_path: 原始图片所在的文件夹路径 [str]
    :param text_data_path: 原始文本数据所在的路径 [str]
    :param save_img_path: 增强后的图片数据所在的路径 [str]
    :param save_text_path: 和增强后的图像数据匹配的文本数据存储路径 [str]
    :param image_size: 图片的像素 [tuple]
    :param augment_count: 每一张图片需要随机增强的倍数 [int]
    :return:
    """
    filename_lst = os.listdir(img_data_path)
    img_lst = [int(x[:-4]) for x in filename_lst]
    filename_lst = [os.path.join(img_data_path, x) for x in filename_lst]

    if not os.path.exists(save_img_path):
        os.mkdir(save_img_path)
        max_index = max(img_lst)
    else:
        saved_img = os.listdir(save_img_path)
        saved_img_num = [int(x[:-4]) for x in saved_img]
        max_index = max(saved_img_num)
    if not os.path.exists(save_text_path):
        os.mkdir(save_text_path)

    # 进行数据增强
    fail = []

    index = max_index + 1
    for img_path, img_num in zip(filename_lst, img_lst):
        try:
            img_path_to_aug = os.path.join(save_img_path,
                                           str(img_num) + '.png')
            text_path_to_aug = os.path.join(save_text_path,
                                            str(img_num) + '.txt')
            # 先判断当前图片是否已经处理过 如果是 则跳过
            if os.path.exists(img_path_to_aug) and\
                    os.path.exists(text_path_to_aug):
                print('{0} is done.'.format(img_num))
                continue

            temp_img = image.load_img(path=img_path, target_size=image_size)
            temp_img = image.img_to_array(temp_img)
            with codecs.open(os.path.join(text_data_path,
                                          str(img_num) + '.txt'),
                             'r',
                             encoding='utf-8') as fr:
                temp_text = fr.read()

            # 先将原始数据存到save的文件夹
            image.save_img(path=img_path_to_aug, x=temp_img)
            with codecs.open(text_path_to_aug, 'w', encoding='utf-8') as ft:
                ft.writelines(temp_text)

            # augment
            for i in range(augment_count):
                # 给每一个新的图像匹配对应文本数据 并存入文件夹
                new_img_path = os.path.join(save_img_path, str(index) + '.png')
                new_text_path = os.path.join(save_text_path,
                                             str(index) + '.txt')
                new_img = image_augment(arr=temp_img)
                image.save_img(path=new_img_path, x=new_img)
                with codecs.open(new_text_path, 'w', encoding='utf-8') as fw:
                    fw.writelines(temp_text)

                index += 1

            print(img_num)
        except:
            fail.append(img_num)
            print('picture {0} is fail.'.format(img_num))
            continue

    return fail
Esempio n. 44
0
    def generate(self, processed_rects, batch_size, verbose=False):
        batch_x = []
        batch_y = [[] for _ in range(2 * len(self.model.shapes))]

        while 1:

            keys = np.array(processed_rects.keys())
            ind = np.arange(len(keys))
            np.random.shuffle(ind)
            rnd = np.random.rand(len(keys))
            empty_name = os.listdir(self.path_empty)
            empty_ind = np.random.randint(len(empty_name), size=len(keys))

            for jj in ind:
                # print jj, rnd[jj]
                if rnd[jj] < self.empty_ratio:
                    processed_key = ''
                else:
                    processed_key = keys[jj]

                #print processed_key

                if processed_key != '' and self.train and processed_key not in self.train:
                    continue

                name = os.path.splitext(processed_key)[0]

                if processed_key == '':
                    img_path = os.path.join(self.path_empty, empty_name[empty_ind[jj]])
                else:
                    img_path = os.path.join(self.path, name + '.jpg')

                img = image.load_img(img_path, target_size=(300, 300))
                img = image.img_to_array(img)
                #img = np.expand_dims(img, axis=0)
                #img = preprocess_input(img)

                if verbose:
                    print img_path

                bboxes, classes = self._generate_for_bbox(processed_rects[processed_key])

                # add extra dimension for each bbox value
                rs = lambda x: np.reshape(x, tuple(list(x.shape) + [1]))

                bboxes_tensors = [rs(np.append(bbox, cls, axis=2)) for bbox, cls in zip(bboxes, classes)]
                classes_tensors = [rs(cls[:, :, ::4]) for cls in classes]
                y = bboxes_tensors + classes_tensors

                batch_x.append(np.array(img))
                for i in range(len(y)):
                    batch_y[i].append(y[i])

                # create Numpy arrays of input data and labels, from each line in the file
                # x, y = process_line(line)
                if len(batch_x) == batch_size:
                    batch_y = list(map(np.array, batch_y))
                    res = {name: value for name, value in zip(self.model.bbox_names + self.model.cls_names, batch_y)}
                    # res['input_1'] = np.array(batch_x)
                    yield ({'input': preprocess_input(np.array(batch_x))}, res)
                    batch_x = []
                    batch_y = [[] for _ in range(2 * len(self.model.shapes))]
Esempio n. 45
0
 def image2x(image_path):
     img = image.load_img(image_path, target_size=(224, 224))
     x = image.img_to_array(img)
     x = np.expand_dims(x, axis=0)
     x = utils.preprocess_input(x, version=1)  # or version=2
     return x
Esempio n. 46
0
training_set = train_datagen.flow_from_directory('dataset/training_set',
                                                 target_size=(64, 64),
                                                 batch_size=32,
                                                 class_mode='binary')

test_set = test_datagen.flow_from_directory('dataset/test_set',
                                            target_size=(64, 64),
                                            batch_size=32,
                                            class_mode='binary')

classifier.fit_generator(training_set,
                         steps_per_epoch=8000,
                         epochs=25,
                         validation_data=test_set,
                         validation_steps=2000)

# Making single predictions

import numpy as np
from keras.preprocessing import image
test_image = image.load_img('dataset/single_prediction/cat_or_dog_1.jpg',
                            target_size=(64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = classifier.predict(test_image)
training_set.class_indices
if result[0][0] == 1:
    prediction = 'dog'
else:
    prediction = 'cat'
Esempio n. 47
0
# best so far 94%
#model_path = '/Users/natewagner/Documents/ML_Final_Project/mask_rcnn_.1588546554.8807812.h5'

from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
#Loading the model in the inference mode
model = modellib.MaskRCNN(
    mode="inference",
    config=config,
    model_dir=
    '/Users/natewagner/Documents/ML_Final_Project/mask_rcnn_.1588559854.111884.h5'
)
# loading the trained weights o the custom dataset
model.load_weights(model_path, by_name=True)
img = load_img(
    "/Users/natewagner/Documents/ML_Final_Project/images/survey-page-B16-52.jpg"
)
#img = load_img("/Users/natewagner/Documents/ML_Final_Project/new_image_arranged.jpg")

img = img_to_array(img)
# detecting objects in the image
results = model.detect([img], verbose=1)

r = results[0]
visualize.display_instances(img,
                            r['rois'],
                            r['masks'],
                            r['class_ids'], ['BG', 'box', 'checked_box'],
                            r['scores'],
                            title="Predictions")
Esempio n. 48
0
def create_adversarial_pattern(input_image, input_label):
    input_label = label
    with tf.GradientTape() as tape:
        tape.watch(input_image)
        prediction = pretrained_model(input_image)
        loss = loss_object(input_label, prediction)

    # Get the gradients of the loss w.r.t to the input image.
    gradient = tape.gradient(loss, input_image)
    # Get the sign of the gradients to create the perturbation
    return tf.sign(gradient)

if __name__ == '__main__':
    for f in files:
        img = image.load_img(f, target_size=(224, 224))
        if img is None:
            continue
        x = image.img_to_array(img)
        x = np.expand_dims(x, axis = 0)
        image_probs = net.predict(x)
        pred = image_probs[0]
        top_inds = pred.argsort()[::-1][:5]

        print(f,' is ',cls_list[top_inds[0]])
        for i in top_inds:
            print('    {:.3f}  {}'.format(pred[i], cls_list[i]))
        print()

        # Get the input label of the image.
        labrador_retriever_index = top_inds[0]
Esempio n. 49
0
#
for item in os.listdir(train_dir):
    image_list.append(item)
np.random.shuffle(image_list)
#创建数组,储存图片信息。结构为(50321, 36, 120, 3),50321代表样本个数,然后是宽度和高度。
# 3代表图片的通道数,如果对图片进行了灰度处理,可以改为单通道 1
X = np.zeros((len(image_list), height, width, 3), dtype = np.uint8)
# 创建数组,储存标签信息
y = np.zeros((len(image_list), word_len * word_class), dtype = np.uint8)

for i,img in enumerate(image_list):
    if i % 10000 == 0:
        print(i)
    img_path = train_dir + "/" + img
    #读取图片
    raw_img = image.load_img(img_path, target_size=(height, width))
    #讲图片转为np数组
    X[i] = image.img_to_array(raw_img)
    #讲标签转换为数组进行保存
    y[i] = captcha_to_vec(img.split('.')[0])

#创建输入,结构为 高,宽,通道
input_tensor = Input( shape=(height, width, 3))

x = input_tensor

#构建卷积网络
#两层卷积层,一层池化层,重复3次。因为生成的验证码比较小,padding使用same
x = Convolution2D(32, 3, padding='same', activation='relu')(x)
x = Convolution2D(32, 3, padding='same', activation='relu')(x)
# x= BatchNormalization()(x)
Esempio n. 50
0
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input, decode_predictions
import keras.preprocessing.image as Image
import numpy as np

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

image_path = "sample_images_pretrain/elephant.jpg"
image = Image.load_img(image_path, target_size=(224, 224))  # imagenet size

x = Image.img_to_array(image)
x = np.expand_dims(x, axis=0)  # add batch size dim
x = preprocess_input(x)

result = model.predict(x)
result = decode_predictions(result, top=3)[0]
print(result[0][1])  # show description
Esempio n. 51
0
 def read_image(filepath, size):
     img = image.load_img(filepath, target_size=size)
     img = image.img_to_array(img)
     return img
Esempio n. 52
0
                             validation_steps=400)

    classifier.save_weights('Multiclassifier.h5')

import numpy as np
from keras.preprocessing import image

from os import listdir
from os.path import isfile, join
path = 'dataset/gameset/'
files = [f for f in listdir(path) if isfile(join(path, f))]

idx = 0
for fl in files:
    # print(fl)
    test_image = image.load_img(path + fl,
                                target_size=(image_size, image_size))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis=0)
    result = classifier.predict(test_image)

    # if idx == 56:
    #     print(result)
    # print(result)
    if result[0][0] > 0.5:
        print(str(idx) + ". 1")
    elif result[0][1] > 0.5:
        print(str(idx) + ". 2")
    elif result[0][2] > 0.5:
        print(str(idx) + ". 3")
    elif result[0][3] > 0.5:
        print(str(idx) + ". 4")
Esempio n. 53
0
    datagen = ImageDataGenerator(
        rotation_range=180,
        width_shift_range=0,
        shear_range=0.3,
        height_shift_range=0,
        zoom_range=[1.3, 1.5],
        horizontal_flip=False,
        fill_mode="nearest",
        channel_shift_range=90,
        brightness_range=[0.7, 1],
        #preprocessing_function=blur
    )

    for i in range(len(images)):
        img = load_img(images[i], color_mode="rgba")
        img = img.resize((416, 416))
        x = img_to_array(img)
        x = np.expand_dims(x, axis=0)
        draw_images(datagen, x, output_dir, i)

class_dict = {
    "background": 0,
    "rasp0w": 10,
    "rasp3a+": 20,
    "rasp3b": 30,
    "rasp3b+": 40,
    "rasp4b": 50
}

try:
Esempio n. 54
0
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
"""
1- ResNet50
"""
model = ResNet50(weights='imagenet', include_top=True)

img_path = "/Users/agambo/deep_learning/tuto/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)

from matplotlib import pyplot
from scipy.misc import toimage

pyplot.subplot(332)
pyplot.imshow(toimage(img))
pyplot.show()

preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=10)[0])
"""
2- VGG 16
"""
from keras.applications.vgg16 import VGG16
# ResNet50 is a powerful model for image classification when it is trained for an adequate number of iterations. We hope you can use what you've learnt and apply it to your own classification problem to perform state-of-the-art accuracy.
#
# Congratulations on finishing this assignment! You've now implemented a state-of-the-art image classification system!

# ## 4 - Test on your own image (Optional/Ungraded)

# If you wish, you can also take a picture of your own hand and see the output of the model. To do this:
#     1. Click on "File" in the upper bar of this notebook, then click "Open" to go on your Coursera Hub.
#     2. Add your image to this Jupyter Notebook's directory, in the "images" folder
#     3. Write your image's name in the following code
#     4. Run the code and check if the algorithm is right!

# In[32]:

img_path = 'images/my_image.jpg'
img = image.load_img(img_path, target_size=(64, 64))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = x / 255.0
print('Input image shape:', x.shape)
my_image = scipy.misc.imread(img_path)
imshow(my_image)
print("class prediction vector [p(0), p(1), p(2), p(3), p(4), p(5)] = ")
print(model.predict(x))

# You can also print a summary of your model by running the following code.

# In[33]:

model.summary()
Esempio n. 56
0
    pyplot.imshow(image)
pyplot.show()

# # Image Preprocessing

# ## Downsizing Images

# Images are downsized to a standard aspect ratio (150 × 150).

# In[16]:

from keras.preprocessing.image import load_img

fig = pyplot.figure(figsize=(15, 4))

image = load_img(A[30])  # original image
fig.add_subplot(1, 2, 1)
pyplot.imshow(image)

image = load_img(A[30], target_size=(150, 150))  # downsized image
fig.add_subplot(1, 2, 2)
pyplot.imshow(image)

pyplot.show()

# ## Converting Images to Arrays

# In[17]:

from keras.preprocessing.image import array_to_img, img_to_array
    'D:/deeplearning/[FreeTutorials.Us] deeplearning/10 Building a CNN/Convolutional_Neural_Networks/dataset/test_set',
    target_size=(64, 64),
    batch_size=32,
    class_mode='binary')

classifier.fit_generator(training_set,
                         steps_per_epoch=8000,
                         epochs=3,
                         validation_data=test_set,
                         validation_steps=2000)
##Prediction Part
import numpy as np
from keras.preprocessing import image

img_pred = image.load_img(
    'D:/deeplearning/[FreeTutorials.Us] deeplearning/10 Building a CNN/Convolutional_Neural_Networks/dataset/single_prediction/cat_or_dog_5.jpg',
    target_size=(64, 64))
img_pred = image.img_to_array(img_pred)
img_pred = np.expand_dims(img_pred, axis=0)
rslt = classifier.predict(img_pred)

ind = training_set.class_indices

if rslt[0][0] == 1:
    prediction = "dog"
else:
    prediction = "cat"

##Save model to json
import os
from keras.models import model_from_json
Esempio n. 58
0
# In[3]:

train = pd.read_csv('train-scene/train.csv')
test = pd.read_csv('test.csv')

# In[4]:

image_path = 'train-scene/train/'

# In[5]:

from scipy.misc import imresize
train_img = []
for i in range(len(train)):
    temp_img = image.load_img(image_path + train['image_name'][i],
                              target_size=(150, 150))
    temp_img = image.img_to_array(temp_img)
    train_img.append(temp_img)

# In[6]:

x_train = np.array(train_img)
#train_img=preprocess_input(train_img)

# In[7]:

test_img = []
for i in range(len(test)):
    temp_img = image.load_img(image_path + test['image_name'][i],
                              target_size=(150, 150))
    temp_img = image.img_to_array(temp_img)
Esempio n. 59
0
def path_to_tensor(img_path):
    img = image.load_img(img_path, target_size=(224, 224))
    x = image.img_to_array(img)
    return np.expand_dims(x, axis=0)
Esempio n. 60
0
datagen = ImageDataGenerator(featurewise_center=False,
                             featurewise_std_normalization=False,
                             preprocessing_function=preprocess)

generator = datagen.flow_from_directory(
    'numbers_train',
    target_size=(48, 48),
    batch_size=1024,  # Only 405 images in directory, so batch always the same
    classes=['02'],
    shuffle=False,
    class_mode='sparse')

inputs, targets = next(generator)

folder = 'numbers_train/02'
files = os.listdir(folder)
files = list(map(lambda x: os.path.join(folder, x), files))

images = []
for f in files:
    img = image.load_img(f)
    #img = img.resize((48, 48))
    img = image.img_to_array(img)
    img = preprocess(img)

    images.append(img)
inputs2 = np.asarray(images)

print(np.mean(inputs))
print(np.mean(inputs2))