Пример #1
0
    def test_img_utils(self):
        height, width = 10, 8

        # Test th data format
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (3, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, data_format='channels_first')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_first')
        assert x.shape == (1, height, width)

        # Test tf data format
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 3)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, data_format='channels_last')
        assert img.size == (width, height)
        x = image.img_to_array(img, data_format='channels_last')
        assert x.shape == (height, width, 1)
Пример #2
0
def explain(model, img, topLabels, numSamples, numFeatures, hideRest, hideColor, positiveOnly):
            
    img, oldImg = transform_img_fn(img)
    img = img*(1./255)
    prediction = model.predict(img)
    explainer = lime_image.LimeImageExplainer()
    img = np.squeeze(img)
    explanation = explainer.explain_instance(img, model.predict, top_labels=topLabels, hide_color=hideColor, num_samples=numSamples)
    temp, mask = explanation.get_image_and_mask(getTopPrediction(prediction[0]), positive_only=positiveOnly, num_features=numFeatures, hide_rest=hideRest)
    tempMask = mask * 255
    temp = Image.fromarray(np.uint8(tempMask))
    temp = temp.resize((oldImg.width, oldImg.height))
    temp = image.img_to_array(temp)
    temp = temp * 1./255
    temp = temp.astype(np.int64)
    temp = np.squeeze(temp)
    oldImgArr = image.img_to_array(oldImg)
    oldImgArr = oldImgArr * (1./255)
    oldImgArr = oldImgArr.astype(np.float64)
    imgExplained = mark_boundaries(oldImgArr, temp)
    imgFinal = np.uint8(imgExplained*255)
    img = Image.fromarray(imgFinal)
    imgByteArr = io.BytesIO()
    img.save(imgByteArr, format='JPEG')
    imgByteArr = imgByteArr.getvalue()


    return imgByteArr
Пример #3
0
    def test_img_utils(self):
        height, width = 10, 8

        # Test th dim ordering
        x = np.random.random((3, height, width))
        img = image.array_to_img(x, dim_ordering='th')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='th')
        assert x.shape == (3, height, width)
        # Test 2D
        x = np.random.random((1, height, width))
        img = image.array_to_img(x, dim_ordering='th')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='th')
        assert x.shape == (1, height, width)

        # Test tf dim ordering
        x = np.random.random((height, width, 3))
        img = image.array_to_img(x, dim_ordering='tf')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='tf')
        assert x.shape == (height, width, 3)
        # Test 2D
        x = np.random.random((height, width, 1))
        img = image.array_to_img(x, dim_ordering='tf')
        assert img.size == (width, height)
        x = image.img_to_array(img, dim_ordering='tf')
        assert x.shape == (height, width, 1)
Пример #4
0
    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.')
Пример #5
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)
Пример #6
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.')
Пример #7
0
    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.')
Пример #8
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))
Пример #9
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))
Пример #10
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.')
Пример #11
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.')
Пример #12
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))                      # 数据增强
Пример #13
0
def crop_image(channels, img, kernel, mode, model, t, x, y, learn_mode='INPUT'):

    if learn_mode == 'INPUT' and model:
        posx = (model.layers[0].input_shape[2] - kernel[0])
        posy = (model.layers[0].input_shape[3] - kernel[1])
    else:
        posx = 0
        posy = 0

    img_c = img.crop((x, y, x + kernel[0] + posx, y + kernel[1] + posy))
    img_converted = img_c
    if mode == 'YCbCr':
        img_converted = img_c.convert('YCbCr')
        ar = img_to_array(img_converted)
        input_array = ar[0].reshape(1, img_c.width, img_c.height)
        t.append(input_array)
    else:
        if channels == 1:
            ar = img_to_array(img_converted)
            for c in range(0, 3):
                ar_new = ar[c].reshape(1, img_converted.width, img_converted.height)
                t.append(ar_new)
        else:
            ar = img_to_array(img_converted)
            t.append(ar)
    return img_c
Пример #14
0
    def convert_image_to_node(self, image, input_node=None):
        from keras.preprocessing.image import img_to_array

        if input_node is None:
            input_node = self.get_input_node(0)

        if input_node['inputType'] == 'image':
            image = image.convert("L")
            image = img_to_array(image)

        elif input_node['inputType'] == 'image_bgr':
            image = image.convert("RGB")
            image = np.asarray(image, dtype='float32')
            image = image[:, :, ::-1].copy()
            image = img_to_array(image)
        else:
            image = image.convert("RGB")
            image = img_to_array(image)

        if 'imageScale' not in input_node:
            input_node['imageScale'] = 255

        if float(input_node['imageScale']) > 0:
            image = image / float(input_node['imageScale'])

        return image
Пример #15
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
Пример #16
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'))
Пример #17
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
Пример #18
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
Пример #19
0
    def test_image_data_generator(self, tmpdir):
        for test_images in self.all_test_images:
            img_list = []
            for im in test_images:
                img_list.append(image.img_to_array(im)[None, ...])

            images = np.vstack(img_list)
            generator = image.ImageDataGenerator(
                featurewise_center=True,
                samplewise_center=True,
                featurewise_std_normalization=True,
                samplewise_std_normalization=True,
                zca_whitening=True,
                rotation_range=90.,
                width_shift_range=0.1,
                height_shift_range=0.1,
                shear_range=0.5,
                zoom_range=0.2,
                channel_shift_range=0.,
                fill_mode='nearest',
                cval=0.5,
                horizontal_flip=True,
                vertical_flip=True)
            generator.fit(images, augment=True)

            for x, y in generator.flow(images, np.arange(images.shape[0]),
                                       shuffle=True, save_to_dir=str(tmpdir)):
                assert x.shape[1:] == images.shape[1:]
                break
def preprocess(imgs):
    imgs_p = np.ndarray((imgs.shape[0], imgs.shape[1], img_rows, img_cols), dtype=np.uint8)
    for i in range(imgs.shape[0]):
        img = Image.fromarray(imgs[i, 0])
        img = img.resize(target_size)
        imgs_p[i, 0] = img_to_array(img, dim_ordering='th')
    return imgs_p
Пример #21
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])
Пример #22
0
def load_data(path, size=224, mode=None):
    img = Image.open(path)
    w,h = img.size
    if w < h:
        if w < size:
            img = img.resize((size, size*h//w))
            w, h = img.size
    else:
        if h < size:
            img = img.resize((size*w//h, size))
            w, h = img.size
    img = img.crop((int((w-size)*0.5), int((h-size)*0.5), int((w+size)*0.5), int((h+size)*0.5)))
    if mode=="original":
        return img

    if mode=="label":
        y = np.array(img, dtype=np.int32)
        mask = y == 255
        y[mask] = 0
        y = binarylab(y, size, 21)
        y = np.expand_dims(y, axis=0)
        return y
    if mode=="data":
        X = image.img_to_array(img)
        X = np.expand_dims(X, axis=0)
        X = preprocess_input(X)
        return X
Пример #23
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))
Пример #24
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))
Пример #25
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)
Пример #26
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
Пример #27
0
    def test_batch_standardize(self):
        # ImageDataGenerator.standardize should work on batches
        for test_images in self.all_test_images:
            img_list = []
            for im in test_images:
                img_list.append(image.img_to_array(im)[None, ...])

            images = np.vstack(img_list)
            generator = image.ImageDataGenerator(
                featurewise_center=True,
                samplewise_center=True,
                featurewise_std_normalization=True,
                samplewise_std_normalization=True,
                zca_whitening=True,
                rotation_range=90.,
                width_shift_range=0.1,
                height_shift_range=0.1,
                shear_range=0.5,
                zoom_range=0.2,
                channel_shift_range=0.,
                brightness_range=(1, 5),
                fill_mode='nearest',
                cval=0.5,
                horizontal_flip=True,
                vertical_flip=True)
            generator.fit(images, augment=True)

            transformed = np.copy(images)
            for i, im in enumerate(transformed):
                transformed[i] = generator.random_transform(im)
            transformed = generator.standardize(transformed)
Пример #28
0
def forward(data, probs_or_score, opts, vars):
    try:
       
        model = vars['model']
        sess = vars['session']
        graph = vars['graph']   

        if model and sess and graph:   

            n_output = len(probs_or_score)
            npdata = np.asarray(data)
            img = Image.fromarray(npdata)
            x = img.resize((opts['image_height'], opts['image_width']))
          
            x = kerasimage.img_to_array(x)
            x = np.expand_dims(x, axis=0)
            x = x*(1./255)
  
            with sess.as_default():
                with graph.as_default():
                    pred = model.predict(x, batch_size=1, verbose=0)

            sanity_check(probs_or_score)
            
            for i in range(len(pred[0])):
                probs_or_score[i] = pred[0][i]  
            return max(probs_or_score)

        else:
            print('Train model first') 
            return 1

    except Exception as e: 
        print_exception(e, 'forward')
        sys.exit()
def predict(args):
    # load the trained convolutional neural network
    print("[INFO] loading network...")
    model = load_model(args["model"])
    
    #load the image
    image = cv2.imread(args["image"])
    orig = image.copy()
     
    # pre-process the image for classification
    image = cv2.resize(image, (norm_size, norm_size))
    image = image.astype("float") / 255.0
    image = img_to_array(image)
    image = np.expand_dims(image, axis=0)
     
    # classify the input image
    result = model.predict(image)[0]
    #print (result.shape)
    proba = np.max(result)
    label = str(np.where(result==proba)[0])
    label = "{}: {:.2f}%".format(label, proba * 100)
    print(label)
    
    if args['show']:   
        # draw the label on the image
        output = imutils.resize(orig, width=400)
        cv2.putText(output, label, (10, 25),cv2.FONT_HERSHEY_SIMPLEX,
            0.7, (0, 255, 0), 2)       
        # show the output image
        cv2.imshow("Output", output)
        cv2.waitKey(0)
Пример #30
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
Пример #31
0
list
del list[5]
list
#convert to series
list_ser = pd.Series(list)

img_list = []
for i in list:
    img_path = img_pth + i
    img_list.append(img_path)
print(len(img_list))
#####################
vgg16_feature_list = []
for i in img_list:
    img = image.load_img(i, target_size=(224, 224))
    img_data = image.img_to_array(img)
    img_data = np.expand_dims(img_data, axis=0)
    img_data = preprocess_input(img_data)
    vgg16_feature = model.predict(img_data)
    vgg16_feature_np = np.array(vgg16_feature)
    vgg16_feature_list.append(vgg16_feature_np.flatten())
print(len(vgg16_feature_list_np))
vgg16_feature_list_np = np.array(vgg16_feature_list)
kmeans = KMeans(n_clusters=3, random_state=0).fit(vgg16_feature_list_np)
y_kmeans = kmeans.predict(vgg16_feature_list_np)
y_kmeans
y_kmeans_list = y_kmeans.tolist()
#convert to series
y_kmeans_se = pd.Series(y_kmeans_list)
cluster_df = pd.DataFrame()
#add values of img_id
Пример #32
0
def prep_mask_to_unet(mask_path):
    mask = kimage.load_img(mask_path)
    mask = kimage.img_to_array(mask)
    mask = mask[:, :, :1] / 255
    # Reads like RGB Image. Hence we take only one frame
    return mask
Пример #33
0
def preprocess_image(_path):
    img = load_img(_path, target_size=(224, 224))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = preprocess_input(img)
    return img
Пример #34
0
while True:
    ret, frame = cap.read()
    cv2.imshow('image', frame)
    key = cv2.waitKey(1)
    if key != -1:
        break

    #===================================================
    #推論する画像を読み込み
    #===================================================
    x = frame
    x = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)  #RBGをRGBに変える
    x = cv2.resize(x, (32, 32))

    #データ形式をCNNモデルに合わせる(3次元のPILから3次元のndarrayへ変更)
    x = img_to_array(x)

    #データを0.0~1.0へ正規化
    x = x.astype('float32') / 255.0

    #次元を合わせる
    x = np.expand_dims(x, axis=0)

    #===================================================
    #推論と結果表示
    #===================================================
    #推論
    preds = model.predict(x)
    print("predicts : " + str(preds))

    #predsのインデックスでソート
Пример #35
0
def process_image(img_path):
	img = image.load_img(img_path, target_size=(224, 224))
	img_array = image.img_to_array(img)
	img_array = np.expand_dims(img_array, axis=0)
	pImg = mobilenet.preprocess_input(img_array)
	return pImg
Пример #36
0
def preprocess_image(image_path):
    img = load_img(image_path, target_size=(img_nrows, img_ncols))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = vgg16.preprocess_input(img)
    return img
Пример #37
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)

# Part 3 - Making new predictions

import numpy as np
from keras.preprocessing import image

test_image = image.load_img('dataset/single_prediction/cat_or_dog_2.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'
Пример #38
0
def draw_img(generator, x, out_dir, img_index):
    save_name = "expand_" + str(img_index)

    g = generator.flow(x,
                       batch_size=1,
                       save_to_dir=out_dir,
                       save_prefix=save_name,
                       save_format="jpg")

    for j in range(11):
        g.next()


if __name__ == "__main__":
    in_dir = "yoji_test"
    out_dir = "yoji_expand"

    if not (os.path.exists(os.path.join("./datasets", out_dir))):
        os.mkdir(os.path.join("./datasets", out_dir))

    images = glob.glob(os.path.join("./datasets", in_dir, "*"))

    generator = ImageDataGenerator(width_shift_range=0.35)

    for i in range(len(images)):
        target_img = load_img(images[i])
        x = img_to_array(target_img)
        x = np.expand_dims(x, axis=0)

        draw_img(generator, x, os.path.join("./datasets", out_dir), i)
Пример #39
0
# print(len(sketch_idx)) #9148


#메모리를 적게 쓰기 위해 uint8로 
sketch = np.zeros((len(sketch_idx),256,256,3),dtype=np.uint8) 
photo = np.zeros((len(sketch_idx),256,256,3),dtype=np.uint8)

from tqdm import tqdm_notebook
from keras.preprocessing.image import img_to_array, load_img
from skimage.transform import resize #사이즈 조절

for i, idx in (enumerate(sketch_idx)):
    # print("i : ",i)
    # print("idx : ",idx)
    img = load_img("./data/sketch/"+idx)
    img = img_to_array(img)
    sketch[i] = img
    

#sketch에 맞춰서 photo를 늘림 
for i, idx in (enumerate(sketch_idx)):
    # print("i : ",i)
    # print("idx : ",idx)
    idx = idx.split("/")[1]+"/"+idx.split("/")[2]
    idx = idx.split("-")[0]
    img = load_img("./data/photo/"+idx+"-removebg-preview"+".png") #배경 제거 후 png로 바뀜 
    img = img_to_array(img)
    photo[i] = img

print(sketch.shape) #(3324, 256, 256, 3)
print(photo.shape) #(3324, 256, 256, 3)
Пример #40
0
    ret, frame = cap.read()
    #input from webcam stored in variable frame
    #ret is just a boolean,telling whether it is able to capture frame or not 
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    #converting frame to grayscale
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)#face detection
    i=0
    for (x,y,w,h) in faces:
        frame=cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2) #rectangle around my face drawn
        roi_gray = gray[y:y+h, x:x+w]#image cropped 
        roi_color = frame[y:y+h, x:x+w]#colored image cropped
        #ret is boolean, to check if frame is coming or not 
        roi_color = cv2.resize(roi_color, (128,128),
                       interpolation=cv2.INTER_AREA)#image resized becz only 128x128 image is to be fed into model

        test_image=image.img_to_array(roi_color)#third dimension added becz input is supposed to be 128x128x3 and not 128x128
        test_image=np.expand_dims(test_image,axis=0)#1 more dimension added because predict function needs 4 dimenions.
        #This dimension takes the batch size. i.e. on how many images are to be fed in model at one time.I kept batch size=1
        result=classifier.predict(test_image)
     
        if (result[0][0] == 1):#result.all to handle multiple faces
            print("angry")
            cv2.putText(frame,"angry",(x,h), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
            #this is how to put text on image
        if (result[0][1] == 1):
            print("sad")
            cv2.putText(frame,"sad",(x,h), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
        if (result[0][2] == 1):
            print("smiling")
            cv2.putText(frame,"smiling",(x,h), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 255, 255), lineType=cv2.LINE_AA)
        cv2.imshow('Input', frame)
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np

PTModel = ResNet50(weights='imagenet')

ImgPath = 'airplane.jpg'
Img = image.load_img(ImgPath, target_size=(224, 224))
InputIMG = image.img_to_array(Img)
InputIMG = np.expand_dims(InputIMG, axis=0)
InputIMG = preprocess_input(InputIMG)

PredData = PTModel.predict(InputIMG)

print('Predicted:', decode_predictions(PredData, top=3)[0])
Пример #42
0
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import decode_predictions
from keras.applications.vgg16 import VGG16
# load the model
model = VGG16()
# load an image from file
image = load_img('cat.jpg', target_size=(224, 224))
# convert the image pixels to a numpy array
image = img_to_array(image)
# reshape data for the model
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
# prepare the image for the VGG model
image = preprocess_input(image)
# predict the probability across all output classes
yhat = model.predict(image)
# convert the probabilities to class labels
label = decode_predictions(yhat)
# retrieve the most likely result, e.g. highest probability
label = label[0][0]
# print the classification
print('%s (%.2f%%)' % (label[1], label[2] * 100))
split = 1

with tqdm(total=num_test_products) as pbar:
    for c, d in enumerate(data):
        product_id = d["_id"]
        num_imgs = len(d["imgs"])

        batch_x = np.zeros((num_imgs, 180, 180, 3), dtype=K.floatx())

        for i in range(num_imgs):
            bson_img = d["imgs"][i]["picture"]

            # Load and preprocess the image.
            img = load_img(io.BytesIO(bson_img), target_size=(180, 180))
            x = img_to_array(img)
            x = test_datagen.random_transform(x)
            x = test_datagen.standardize(x)

            # Add the image to the batch.
            batch_x[i] = x

        prediction = model.predict(batch_x, batch_size=num_imgs)
        avg_pred = prediction.mean(axis=0)

        test_product_score.append(avg_pred)

        if (len(test_product_score) % 150000 == 0):
            print('number of products:{}'.format(len(test_product_score)))
            print('saving up to product_id:{}'.format(product_id))
            print('split:{}'.format(split))
Пример #44
0
from keras.utils import np_utils
from keras.datasets import mnist
from keras.preprocessing.image import img_to_array, array_to_img

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], 784)
x_test = x_test.reshape(x_test.shape[0], 784)
x_train = np.dstack([x_train] * 3)
x_test = np.dstack([x_test] * 3)
x_train = x_train.reshape(x_train.shape[0], 28, 28, 3).astype('float32') / 255
x_test = x_test.reshape(x_test.shape[0], 28, 28, 3).astype('float32') / 255

# reshape 28 => 48 증폭
x_train = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((48, 48)))
    for im in x_train
])
x_test = np.asarray([
    img_to_array(array_to_img(im, scale=False).resize((48, 48)))
    for im in x_test
])

print(x_train.shape)
print(x_test.shape)

y_train = np_utils.to_categorical(y_train)
y_test = np_utils.to_categorical(y_test)

from keras.applications import xception
conv_base = xception(weights='imagenet',
Пример #45
0
def load_image(image_path, grayscale=False, target_size=None):
    pil_image = image.load_img(image_path, grayscale, target_size)
    return image.img_to_array(pil_image)
Пример #46
0
from matplotlib import pyplot as plt
from keras.preprocessing import image
from keras import models
from keras.models import model_from_json
import numpy as np

json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
loaded_model.load_weights("model.h5")

img_path = 'dataset/patterns/trees_4.png'
img = image.load_img(img_path, target_size=(120, 120))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
plt.imshow(img_tensor[0])
plt.show()
print(img_tensor.shape)

layer_outputs = [layer.output for layer in loaded_model.layers[:16]]
# Extracts the outputs of the top 12 layers
activation_model = models.Model(
    inputs=loaded_model.input, outputs=layer_outputs
)  # Creates a model that will return these outputs, given the model input
activations = activation_model.predict(img_tensor)

layer_names = []
for layer in loaded_model.layers[:16]:
def load_data(image_num, data_number, seq_len):
    traingen = ImageDataGenerator(rescale=1. / 255)
    # 生成图片对
    print('loading data.....')
    frame_num = image_num
    train_data1 = []
    train_data2 = []
    train_lab = []
    count = 0
    while count < data_number:
        count = count + 1
        save_path = os.path.join(data_path, str(count) + '.pkl')
        print("Generating the ", count, "th image pair")
        pos_neg = np.random.randint(0, 2)
        if pos_neg == 0:
            seed1 = np.random.randint(0, seq_len)
            seed2 = np.random.randint(0, seq_len)
            while seed1 == seed2:
                seed1 = np.random.randint(0, seq_len)
                seed2 = np.random.randint(0, seq_len)
            frame1 = np.random.randint(0, frame_num)
            frame2 = np.random.randint(0, frame_num)
            path1 = os.path.join(root_path, str(seed1), str(frame1) + '.jpg')
            path2 = os.path.join(root_path, str(seed2), str(frame2) + '.jpg')
            image1 = img_to_array(load_img(path1))
            image1 = image1.reshape((1, ) + image1.shape)
            for new_img in traingen.flow(image1, batch_size=1, shuffle=False):
                image1 = new_img
                break
            image2 = img_to_array(load_img(path2))
            image2 = image2.reshape((1, ) + image2.shape)
            for new_img in traingen.flow(image2, batch_size=1, shuffle=False):
                image2 = new_img
                break
            train_data1 = np.squeeze(image1)
            train_data2 = np.squeeze(image2)
            train_lab = np.array(0)
            joblib.dump((train_data1, train_data2, train_lab),
                        save_path,
                        compress=0,
                        protocol=4)
        else:
            seed = np.random.randint(0, seq_len)
            frame1 = np.random.randint(0, frame_num)
            frame2 = np.random.randint(0, frame_num)
            path1 = os.path.join(root_path, str(seed), str(frame1) + '.jpg')
            path2 = os.path.join(root_path, str(seed), str(frame2) + '.jpg')
            image1 = img_to_array(load_img(path1))
            image1 = image1.reshape((1, ) + image1.shape)
            for new_img in traingen.flow(image1, batch_size=1, shuffle=False):
                image1 = new_img
                break
            image2 = img_to_array(load_img(path2))
            image2 = image2.reshape((1, ) + image2.shape)
            for new_img in traingen.flow(image2, batch_size=1, shuffle=False):
                image2 = new_img
                break
            train_data1 = np.squeeze(image1)
            train_data2 = np.squeeze(image2)
            train_lab = np.array(1)
            joblib.dump((train_data1, train_data2, train_lab),
                        save_path,
                        compress=0,
                        protocol=4)
    print("loading finish!")
    return [np.array(train_data1), np.array(train_data2), np.array(train_lab)]
Пример #48
0
def main(modulename, imagename):
    '''
    Kera的应用模块Application提供了带有预训练权重的Keras模型,这些模型可以用来进行预测、特征提取和finetune
    模型的预训练权重将下载到~/.keras/models/并在载入模型时自动载入
    '''

    # 设置为测试模式
    keras.backend.set_learning_phase(0)

    model = ResNet50(weights=modulename)

    logging.info(model.summary())

    img = image.load_img(imagename, target_size=(224, 224))
    imagedata = image.img_to_array(img)
    #imagedata=imagedata[:, :, ::-1]
    imagedata = np.expand_dims(imagedata, axis=0)

    #logit fc1000
    logits = model.get_layer('fc1000').output

    #keras中获取指定层的方法为:
    #base_model.get_layer('block4_pool').output)
    # advbox demo
    # 因为原始数据没有归一化  所以bounds=(0, 255)  KerasMode内部在进行预测和计算梯度时会进行预处理
    # imagenet数据集归一化时 标准差为1  mean为[104, 116, 123]
    m = KerasModel(model,
                   model.input,
                   None,
                   logits,
                   None,
                   bounds=(0, 255),
                   channel_axis=3,
                   preprocess=([104, 116, 123], 1),
                   featurefqueezing_bit_depth=8)

    attack = DeepFoolAttack(m)
    attack_config = {"iterations": 100, "overshoot": 10}

    #y设置为空 会自动计算
    adversary = Adversary(imagedata[:, :, ::-1], None)

    # deepfool non-targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)
        #强制类型转换 之前是float 现在要转换成iunt8

        #::-1 reverses the color channels, because Keras ResNet50 expects BGR instead of RGB
        adversary_image = adversary_image[:, :, ::-1]

        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [224, 224, 3])

        logging.info(adversary_image - imagedata)
        img = array_to_img(adversary_image)
        img.save('adversary_image_nontarget.jpg')

    print("deepfool non-target attack done")

    attack = DeepFoolAttack(m)
    attack_config = {"iterations": 100, "overshoot": 10}

    adversary = Adversary(imagedata[:, :, ::-1], None)

    tlabel = 489
    adversary.set_target(is_targeted_attack=True, target_label=tlabel)

    # deepfool targeted attack
    adversary = attack(adversary, **attack_config)

    if adversary.is_successful():
        print('attack success, adversarial_label=%d' %
              (adversary.adversarial_label))

        #对抗样本保存在adversary.adversarial_example
        adversary_image = np.copy(adversary.adversarial_example)
        #强制类型转换 之前是float 现在要转换成int8

        #::-1 reverses the color channels, because Keras ResNet50 expects BGR instead of RGB
        adversary_image = adversary_image[:, :, ::-1]

        adversary_image = np.array(adversary_image).astype("uint8").reshape(
            [224, 224, 3])

        logging.info(adversary_image - imagedata)
        img = array_to_img(adversary_image)
        img.save('adversary_image_target.jpg')

    print("deepfool target attack done")
Пример #49
0
def predictor():
    from keras import backend as K
    import numpy as np
    from keras.preprocessing import image
    test_image = image.load_img(r"G:\\Code_dataset\1.png",
                                target_size=(64, 64))
    test_image = image.img_to_array(test_image)
    test_image = np.expand_dims(test_image, axis=0)
    #classifier = load_model(r'G:\\Code_dataset\Handgesture_Final_FCN.h5')
    classifier = load_model(r'G:\\Code_dataset\Handgesture_Final_CNN.h5')
    #classifier = load_model(r'G:\\Code_dataset\Handgesture_Final_VGG16.h5')

    result = classifier.predict(test_image)
    K.clear_session()
    # print(result)
    if result[0][0] == max(result[0]):
        return ' Zero  '
    elif result[0][1] == max(result[0]):
        return ' One  '
    elif result[0][2] == max(result[0]):
        return ' Two  '
    elif result[0][3] == max(result[0]):
        return ' Three  '
    elif result[0][4] == max(result[0]):
        return '  Four  '
    elif result[0][5] == max(result[0]):
        return 'Five'
    elif result[0][6] == max(result[0]):
        return 'Six'
    elif result[0][7] == max(result[0]):
        return 'Seven'
    elif result[0][8] == max(result[0]):
        return 'Eight'
    elif result[0][9] == max(result[0]):
        return 'Nine'
    elif result[0][10] == max(result[0]):
        return 'A'
    elif result[0][11] == max(result[0]):
        return 'B'
    elif result[0][12] == max(result[0]):
        return 'C'
    elif result[0][13] == max(result[0]):
        return 'D'
    elif result[0][14] == max(result[0]):
        return 'E'
    elif result[0][15] == max(result[0]):
        return 'F'
    elif result[0][16] == max(result[0]):
        return 'I'
    elif result[0][17] == max(result[0]):
        return 'J'
    elif result[0][18] == max(result[0]):
        return 'K'
    elif result[0][19] == max(result[0]):
        return 'L'
    elif result[0][20] == max(result[0]):
        return 'U'
    elif result[0][21] == max(result[0]):
        return 'M'
    elif result[0][22] == max(result[0]):
        return 'N'
    elif result[0][23] == max(result[0]):
        return 'O'
    elif result[0][24] == max(result[0]):
        return 'P'
    elif result[0][26] == max(result[0]):
        return 'Q'
    elif result[0][27] == max(result[0]):
        return 'R'
    elif result[0][28] == max(result[0]):
        return 'S'
    elif result[0][29] == max(result[0]):
        return 'T'
    elif result[0][30] == max(result[0]):
        return 'U'
    elif result[0][31] == max(result[0]):
        return 'V'
    elif result[0][32] == max(result[0]):
        return 'W'
    elif result[0][33] == max(result[0]):
        return 'X'
    elif result[0][34] == max(result[0]):
        return 'Y'
    elif result[0][35] == max(result[0]):
        return 'Z'
Пример #50
0
def main():
    data_root = os.path.join('data', 'gen')
    train_img_list = [
        fn for fn in os.listdir(data_root) if fn.endswith('.png')
    ]
    x_data = []
    for train_fn in train_img_list:
        img = image.load_img(os.path.join(data_root, train_fn))
        x = image.img_to_array(img) / 255.0
        x_data.append(x)
    x_data = np.array(x_data)
    print(x_data.shape)

    x_input = Input(shape=(IN_IMG_WIDTH, IN_IMG_WIDTH, 3))
    x = x_input

    x = Conv2D(filters=32,
               kernel_size=3,
               strides=1,
               padding='same',
               activation='relu')(x)
    x = MaxPooling2D(pool_size=2, padding='same')(x)
    x = Conv2D(filters=8,
               kernel_size=3,
               strides=1,
               padding='same',
               activation='relu')(x)
    x = MaxPooling2D(pool_size=2, padding='same')(x)
    x = Conv2D(filters=4,
               kernel_size=3,
               strides=1,
               padding='same',
               activation='relu')(x)
    x = MaxPooling2D(pool_size=2, padding='same')(x)

    mid_layer = x
    mid_shape = K.int_shape(x)
    print(mid_shape)

    encoder_model = Model(x_input, mid_layer, name='encoder')
    encoder_model.summary()

    decoder_input = Input(shape=(mid_shape[1], mid_shape[2], mid_shape[3]))
    x = decoder_input

    x = Conv2DTranspose(filters=4,
                        kernel_size=3,
                        strides=1,
                        padding='same',
                        activation='relu')(x)
    x = UpSampling2D(size=2)(x)
    x = Conv2DTranspose(filters=8,
                        kernel_size=3,
                        strides=1,
                        padding='same',
                        activation='relu')(x)
    x = UpSampling2D(size=2)(x)
    x = Conv2DTranspose(filters=32,
                        kernel_size=3,
                        strides=1,
                        padding='same',
                        activation='relu')(x)
    x = UpSampling2D(size=2)(x)
    x = Conv2DTranspose(filters=3,
                        kernel_size=3,
                        strides=1,
                        padding='same',
                        activation='relu')(x)
    outputs = x

    decoder_model = Model(decoder_input, outputs, name='decoder')
    decoder_model.summary()

    ae_model = Model(x_input,
                     decoder_model(encoder_model(x_input)),
                     name='auto_encoder')
    ae_model.summary()

    ae_model.compile(loss='mse', optimizer=Adam(lr=LEARNING_RATE, decay=DECAY))
    ae_model.fit(x_data, x_data, batch_size=BATCH_SIZE, epochs=EPOCHS)

    x_decoded = ae_model.predict(x_data)
    show_image(x_data[0])
    show_image(x_decoded[0])
    print(x_decoded[0][1, 1])
Пример #51
0
def read_img(filepath, size):
    img = image.load_img(filepath, target_size=size)
    img = image.img_to_array(img)
    return img
def train(input_dir,input_dir_ParsedVMAF):

    def crop_center(img, crop_width, crop_height):
         width, height = img.size
         return img.crop(((width - crop_width) // 2,
                              (height - crop_height) // 2,
                              (width + crop_width) // 2,
                              (height + crop_height) // 2))
         
    def Crop_Function (img, crop_width, crop_height,i):
        width, height = img.size
        for col_i in range(0, width, crop_width):
            for row_i in range(0, height, crop_height):
                vmafs_new.append(vmafs[i])
                return img.crop((col_i, row_i, col_i + crop_width, row_i + crop_height))
       
    def resize_half(img):
         width, height = img.size
         return img.resize((width//2,height//2))
         
         
    img=[]
    vmafs=[]
    outputImage = []
    vmafs_new=[]
    imgnames=[]
    
    
    filenames = glob.glob(input_dir+'/*.png')
    for f in filenames:
        f=os.path.basename(f)
        img.append(f)
    
    df = pd.read_csv (input_dir_ParsedVMAF)
    df_list=df.values.tolist()
    for i in range(0, len(img)):
        for j in range(0, len(df_list)):
            if (img[i]==df_list[j][0]):
                imgs=img[i]
                Vmaf=df_list[j][1]
                imgnames.append(imgs)
                vmafs.append(Vmaf)
                
#    vmafs_array=np.asarray(vmafs)
    
    for i in range(len(imgnames)):
        im = Image.open(input_dir + '/' + imgnames[i] )
        width, height = im.size
        im_cropped =Crop_Function(im, 256, 256,i) 
    #   im_resized=resize_half(im)
    #   im_resized_cropped =Crop_Function(im_resized, 256, 256,i)
        im_array=img_to_array(im_cropped)
    #   im_array_resized=img_to_array(im_resized_cropped)
        norm_image =im_array / 255.0
    #   norm_image_resized =im_array_resized / 255.0
        outputImage.append(norm_image)
    #   outputImage.append(norm_image_resized)
    
    outputImage_array=np.asarray(outputImage)
    #vmafs_array = vmafs_array.reshape(-1, 1)    
    vmafs_new_array=np.asarray(vmafs_new)
    
    split = train_test_split(outputImage_array, vmafs_new_array, test_size=0.25, random_state=42)
    (trainX, testX, trainy, testy) = split
    
    trainX = np.asarray(trainX)
    testX = np.asarray(testX)
    
    model=Basic_CNN.create_cnn(256, 256, 3,regress=True) 
    opt = Adam(lr=1e-3, decay=1e-3 / 200)
    model.compile(loss="mean_absolute_percentage_error", optimizer=opt)
      
    print("[INFO] training model...")
    model.fit(trainX, trainy, validation_data=(testX, testy), epochs=300, batch_size=32)
    
    print("[INFO] predicting Vmaf...")
    preds = model.predict(testX)
    
    plt.pyplot.scatter(preds,testy)
    
    pkl_filename = "pickle_model.pkl"
    with open(pkl_filename, 'wb') as file:
        pickle.dump(model, file)    
Пример #53
0
    img = plt.imread("../input/face-mask-detection-data/without_mask/" + i)
    plt.imshow(img)
    plt.title("Without mask")
    break

#read images from the dataset
from keras.preprocessing.image import load_img, img_to_array
from numpy import asarray
from PIL import Image
x = []
y = []
filenames = (os.listdir("../input/face-mask-detection-data/with_mask"))
for i in filenames:
    photo = load_img("../input/face-mask-detection-data/with_mask/" + i,
                     target_size=(128, 128))
    photo = img_to_array(photo)
    x.append(photo)
    y.append(1)  # 1 with mask
filenames = (os.listdir("../input/face-mask-detection-data/without_mask"))
for i in filenames:
    photo = load_img("../input/face-mask-detection-data/without_mask/" + i,
                     target_size=(128, 128))
    photo = img_to_array(photo)
    x.append(photo)
    y.append(0)  # 0 without mask
x = asarray(x)

#split the data into train and test
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x,
                                                    y,
Пример #54
0
def predict_mouth(image, count, model, mouthLB):
    # Load the Haar cascade files for face and eye
    face_cascade = cv2.CascadeClassifier(
        'eye_extraction/haarcascade_frontalface_default.xml')
    mouth_cascade = cv2.CascadeClassifier(
        'mouth_extraction/haarcascade_mcs_mouth.xml')

    # Check if the face cascade file has been loaded correctly
    if face_cascade.empty():
        raise IOError('Unable to load the face cascade classifier xml file')

    # Check if the eye cascade file has been loaded correctly
    if mouth_cascade.empty():
        raise IOError('Unable to load the eye cascade classifier xml file')

    # Initialize the object
    frame = image
    new_image = []
    # Define the scaling factor
    ds_factor = 0.5

    # Resize the frame
    frame = cv2.resize(frame,
                       None,
                       fx=ds_factor,
                       fy=ds_factor,
                       interpolation=cv2.INTER_AREA)

    # Convert to grayscale
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    # Run the face detector on the grayscale image
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    # For each face that's detected, run the eye detector
    for (x, y, w, h) in faces:
        # Extract the grayscale face ROI
        roi_gray = gray[y:y + h, x:x + w]

        # Extract the color face ROI
        roi_color = frame[y:y + h, x:x + w]

        # Run the eye detector on the grayscale ROI
        mouths = mouth_cascade.detectMultiScale(roi_gray)
        count = 0
        for (x_eye, y_eye, w_eye, h_eye) in mouths:
            new_image = roi_color[y_eye:y_eye + w_eye, x_eye:x_eye + h_eye]
            cv2.imwrite("output_new/mouth_Detector_" + str(count) + ".jpg",
                        new_image)
            count = count + 1
            break

    if len(new_image) == 0:
        return "None", 0.0
    new_image = cv2.resize(new_image, (64, 64))
    new_image = new_image.astype("float") / 255.0
    new_image = img_to_array(new_image)
    new_image = np.expand_dims(new_image, axis=0)

    # classify the input image using Keras' multi-output functionality
    #print("[INFO] classifying image...")
    prob = model.predict(new_image)
    #print("The predicted probability (mouth) is ",prob)
    mouthLabel = mouthLB.classes_
    #print("The mouth labels are : ",mouthLabel)
    #mouthText = "head: {} ({:.2f}%)".format(mouthLabel, mouthProba[0][mouthIdx] * 100)
    #cv2.putText(output, mouthText, (10, 55), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)

    # display the predictions to the terminal as well
    if prob < 0.5:
        #print("Mouth Prediction : ",mouthLabel[0],prob)
        return (mouthLabel[0], prob)
    else:
        #print("Mouth Prediction : ",mouthLabel[1],prob)
        return (mouthLabel[1], prob)
Пример #55
0
print(history.history.keys())

plt.figure()
plt.plot(history.history['acc'], 'orange', label='Training accuracy')
plt.plot(history.history['val_acc'], 'blue', label='Validation accuracy')
plt.plot(history.history['loss'], 'red', label='Training loss')
plt.plot(history.history['val_loss'], 'green', label='Validation loss')
plt.legend()

img_path = './Open_I_abd_vs_CXRs/TEST/chest2.png'  #change to location of chest x-ray
img_path2 = './Open_I_abd_vs_CXRs/TEST/abd2.png'  #change to location of abd x-ray
img = image.load_img(img_path, target_size=(img_width, img_height))
img2 = image.load_img(img_path2, target_size=(img_width, img_height))

img = image.img_to_array(img)
x = np.expand_dims(img, axis=0) * 1. / 255
score = model.predict(x)
print('Predicted:', score, 'Chest X-ray' if score < 0.5 else 'Abd X-ray')

plt.figure()
plt.imshow(img)

plt.figure()
plt.imshow(img2)

img2 = image.img_to_array(img2)
x = np.expand_dims(img2, axis=0) * 1. / 255
score2 = model.predict(x)
print('Predicted:', score2, 'Chest X-ray' if score2 < 0.5 else 'Abd X-ray')
def preprocess_image(image_path):
    img = load_img(image_path, target_size = (img_height,img_width))
    img = img_to_array(img)
    img = np.expand_dims(img,axis = 0)
    img = vgg19.preprocess_input(img)
    return img
Пример #57
0
    plt.imshow(x_org)
    plt.show()
    plt.imshow(x_pos)
    plt.show()
    # x_pos = np.expand_dims(x_pos, axis=0)
    # x_pos = preprocess_input(x_pos)

    # pred_pos = base_model.predict(x_pos)

    # print("Un-poisoned image: {}".format(decode_predictions(pred, top=3)[0]))
    # print("Advserial image: {}".format(decode_predictions(pred_pos,top=3)[0]))
=======
img_orginal = image.load_img(img_path)
img = image.load_img(img_path, target_size=(224, 224))
x_org = image.img_to_array(img_orginal)
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

pred = base_model.predict(x)

x_pos = fast_signed_gradient(x, rand_other(pred))
x_pos = x_pos + x

# x_pos = original_img_peturb(x_org,x_pos)
# plt.imshow(x_org[:,:,::-1])
# plt.show()
# plt.imshow(x_pos[:,:,::-1])
# plt.show()
x_pos = preprocess_input(x_pos)
Пример #58
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
Пример #59
0
base_dir = '/Users/takaishikeito/Documents/DLDatasets/cats_and_dog_small'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
train_cats_dir = os.path.join(train_dir, 'cats')

fnames = [
    os.path.join(train_cats_dir, fname) for fname in os.listdir(train_cats_dir)
]

print(len(fnames))  #1000

img_path = fnames[1]

img = image.load_img(img_path, target_size=(150, 150))
print(str(img))  # RGB 150 * 150

#(150, 150, 3)
x = image.img_to_array(img)
#(1, 150, 150, 3)
x = x.reshape((1, ) + x.shape)
#print(x)

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()
def load_processed_image_data(path):
    img = image.load_img(path, target_size=IMAGE_DIMS)
    img_array = image.img_to_array(img)
    return preprocess_input(img_array)