def next_batch(self, n): if self.data_idx is None: self._initialize_dataset() batch_x = [None] * n batch_y = [None] * n for i in range(n): batch_x_file_path, batch_y_file_path = self.data_file_paths[ self.data_idx] x = utils.read_image(batch_x_file_path, size=(self.image_height, self.image_width)) batch_x[i] = x.astype( np.float32) / 255. - self.dataset.mean_pixel() y = utils.read_image(batch_y_file_path, mode='I', size=(self.image_height, self.image_width)).astype('int32') y = np.expand_dims(y, 3) batch_y[i] = y self.data_idx += 1 if self.data_idx == len(self.data_file_paths): self.data_idx = 0 shuffle(self.data_file_paths) return batch_x, batch_y
def style_transfer(content_name, style_name, output_name, content_weight, style_weight, tv_weight, pooling, learning_rate, beta1, beta2, epsilon, max_iteration, check_point): time_start = time.time() # read images content = read_image(PATH_INPUT_CONTENT + content_name) style = read_image(PATH_INPUT_STYLE + style_name) style = scipy.misc.imresize(style, content.shape[1] / style.shape[1]) # initialize objects vgg = VGG(TRAINED_NETWORK_DATA, pooling) nn = NeuralNetwork(content, style, vgg, content_weight, style_weight, tv_weight) # train model for k, output_image in nn.train_model(learning_rate, beta1, beta2, epsilon, max_iteration, check_point): name_list = output_name.split('.') image_name = PATH_OUTPUT + '.'.join(name_list[:-1]) + '_{}.{}'.format( str(k) if not k % check_point else 'final', name_list[-1]) save_image(output_image, image_name) time_end = time.time() logger.info('Time elapsed: {} seconds'.format(round(time_end - time_start)))
def train_model(self): if not os.path.exists(self.MODEL_NAME+'_result'): os.mkdir(self.MODEL_NAME+'_result') if not os.path.exists(self.LOGS_DIR): os.mkdir(self.LOGS_DIR) if not os.path.exists(self.CKPT_DIR): os.mkdir(self.CKPT_DIR) if not os.path.exists(self.OUTPUT_DIR): os.mkdir(self.OUTPUT_DIR) train_set_path = read_data_path(self.TRAIN_IMAGE_PATH, self.TRAIN_LABEL_PATH) valid_set_path = read_data_path(self.VALID_IMAGE_PATH, self.VALID_LABEL_PATH) ckpt_save_path = os.path.join(self.CKPT_DIR, self.MODEL_NAME+'_'+str(self.N_BATCH)+'_'+str(self.LEARNING_RATE)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) total_batch = int(len(train_set_path) / self.N_BATCH) counter = 0 self.saver = tf.train.Saver() self.writer = tf.summary.FileWriter(self.LOGS_DIR, sess.graph) for epoch in tqdm(range(self.N_EPOCH)): total_loss = 0 random.shuffle(train_set_path) # 매 epoch마다 데이터셋 shuffling random.shuffle(valid_set_path) for i in range(int(len(train_set_path) / self.N_BATCH)): # print(i) batch_xs_path, batch_ys_path = next_batch(train_set_path, self.N_BATCH, i) batch_xs = read_image(batch_xs_path, [self.RESIZE, self.RESIZE]) batch_ys = read_annotation(batch_ys_path, [self.RESIZE, self.RESIZE]) feed_dict = {self.input_x: batch_xs, self.label_y: batch_ys, self.is_train: True} _, summary_str ,loss = sess.run([self.optimizer, self.loss_summary, self.loss], feed_dict=feed_dict) self.writer.add_summary(summary_str, counter) counter += 1 total_loss += loss ## validation 과정 valid_xs_path, valid_ys_path = next_batch(valid_set_path, 4, 0) valid_xs = read_image(valid_xs_path, [self.RESIZE, self.RESIZE]) valid_ys = read_annotation(valid_ys_path, [self.RESIZE, self.RESIZE]) valid_pred = sess.run(self.pred, feed_dict={self.input_x: valid_xs, self.label_y: valid_ys, self.is_train:False}) valid_pred = np.squeeze(valid_pred, axis=3) valid_ys = np.squeeze(valid_ys, axis=3) ## plotting and save figure img_save_path = self.OUTPUT_DIR + '/' + str(epoch).zfill(3) + '.png' draw_plot_segmentation(img_save_path, valid_xs, valid_pred, valid_ys) print('\nEpoch:', '%03d' % (epoch + 1), 'Avg Loss: {:.6}\t'.format(total_loss / total_batch)) self.saver.save(sess, ckpt_save_path+'_'+str(epoch)+'.model', global_step=counter) self.saver.save(sess, ckpt_save_path+'_'+str(epoch)+'.model', global_step=counter) print('Finish save model')
def main(): img1 = utils.read_image('lenna-denoise.png') img2 = utils.read_image('results/task2_result.jpg') error = np.sum((img1.astype("float") - img2.astype("float"))**2) error = error / float(img1.shape[0] * img2.shape[1]) print(error)
def generate_images(paths, color_mode=cv2.IMREAD_COLOR, tgt_size=None, method='resize', check_compatibility=False, compatibility_multiplier=32, seed=None): '''Wrapper for read_image multiple times''' ret = [] seeds = [] for path in paths: if method == 'crop': ret.extend( read_image(path, color_mode=color_mode, target_size=tgt_size, method=method, seed=seed)) elif method == 'random_crop': im, seed = read_image(path, color_mode=color_mode, target_size=tgt_size, method=method, seed=seed) ret.append(im) seeds.append(seed) elif method == 'resize': ret.append( read_image(path, color_mode=color_mode, target_size=tgt_size, method=method, seed=seed)) elif method is None: ret.append( read_image(path, color_mode=color_mode, target_size=None, method=method, check_compatibility=check_compatibility, compatibility_multiplier=compatibility_multiplier, seed=seed)) ret = np.array(ret) if len(ret.shape) == 3: ret = np.expand_dims(ret, axis=-1) ret = normalize(ret, type_=0) if method == 'random_crop': return ret, seeds else: return ret
def load_data(im1_filename, im2_filename, flo_filename): """ Loads images and flow ground truth. Returns 4D tensors.""" # load images as numpy array img1 = rgb2gray(read_image(im1_filename)) img2 = rgb2gray(read_image(im2_filename)) flo = read_flo(flo_filename) # convert to torch 4D tensor tensor1 = numpy2torch(img1).unsqueeze_(0) tensor2 = numpy2torch(img2).unsqueeze_(0) flow_gt = numpy2torch(flo).unsqueeze_(0) return tensor1, tensor2, flow_gt
def combine_fig(file_name, salience_dir, parsing_dir, salience_parsing_dir, save_dir): salience_file = osp.join(salience_dir, file_name) parsing_file = osp.join(parsing_dir, file_name) salience_parsing_file = osp.join(salience_parsing_dir, file_name) save_file = osp.join(save_dir, file_name) images = [ read_image(salience_file), read_image(parsing_file), read_image(salience_parsing_file) ] plot(images, save_file)
def train(): notcars = glob.glob('data/non-vehicles/*/*.png') cars = glob.glob('data/vehicles/*/*.png') print_stats(cars, notcars) features_car = [] for car in cars: img = read_image(car) img_processed = process_image(img) features_car.append(extract_features(img_processed, parameters)) features_notcar = [] for notcar in notcars: img = read_image(notcar) img_processed = process_image(img) # png features_notcar.append(extract_features(img_processed, parameters)) features = np.vstack((features_car, features_notcar)) # Fit a per-column scaler scaler = StandardScaler().fit(features) # Apply the scaler to X features_scaled = scaler.transform(features) # Define the labels vector labels = np.hstack( (np.ones(len(features_car)), np.zeros(len(features_notcar)))) # Split up data into randomized training and test sets rand_state = np.random.randint(0, 100) out = train_test_split(features_scaled, labels, test_size=0.2, random_state=rand_state) features_train, features_test, labels_train, labels_test = out # Initialize support vector machine object clf = SVC(kernel='linear', C=0.00001) # Check the training time for the SVC t = time.time() clf.fit(features_train, labels_train) print('{0:2.2f} seconds to train SVC...'.format(time.time() - t)) # Accuracy score accuracy = clf.score(features_test, labels_test) print('Test Accuracy of SVC = {0:2.4f}'.format(accuracy)) classifier = Classifier(clf, scaler) joblib.dump(classifier, 'classifier.pkl') return classifier
def load_data(load_from, img_folder, gt_img_folder): ims = list(map(str.strip, open(load_from, 'r').readlines())) tr = [] tr_gt = [] for im in ims: img = utils.read_image(img_folder + "/" + im) if gt_img_folder: gt_img = utils.read_image(gt_img_folder + "/" + im) L, _ = utils.cvt2Lab(img) if gt_img_folder: _, ab = utils.cvt2Lab(gt_img) tr.append(L) if gt_img_folder: tr_gt.append(ab) return np.array(tr), np.array(tr_gt)
def test_compare_images(img1_filename, img2_filename): fp = utils.FaceProcessor() img1 = fp.process_image(utils.read_image(img1_filename)) img2 = utils.read_image(img2_filename) print img1 print "---" print img2 print img1.shape, ", ", img2.shape img3 = img1 - img2 print img3.sum() # allt núll, nákvæmlega eins img3 = img2 - img1 print img3.sum() # allt núll, nákvæmlega eins
def Haze_Remover(path=None, image=None): ''' This function is used to dehaze a image from an image path or from a cv2 image object ''' if path is None and image is None: print( "There is not path and image enter to the function. Please add a image or a path to the model" ) return None else: if image is none: image = read_image(path) min_image = min_filter(image) dark_prior = rgb_min_image(min_image) A = A_estimator(image, dark_prior) img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Transmition_image = self.transmission_map(image, A, 0.95) refine_Transmission_image = self.guided_filter( img_gray.astype(np.float32), Transmition_image.astype(np.float32), 100, self.epsilon) refine_radience_image = self.Radience_cal( image, A, refine_Transmission_image, 0.1) self.output = { 'Input': image, 'Min_Image': min_image, 'A': A_estimator, 'Gray_Image': img_gray, 'Transmition_Map': Transmition_image, 'Refine_Transmition_Map': refine_Transmission_image, 'DeHaze_Image': refine_radience_image } return output
def __load_data(self): """ Load all the images in the folder """ print('Loading data...') examples = [] count = 0 skipped = 0 files = os.listdir(self.examples_path) for i in range(10): random.shuffle(files) for f in files: if len(f.split('_')[0]) > self.max_char_count: continue arr, initial_len = read_image( os.path.join(self.examples_path, f) ) examples.append( ( arr, f.split('_')[0], label_to_array(f.split('_')[0], self.char_vector) ) ) #print(f.split('_')[0], label_to_array(f.split('_')[0], self.char_vector)) count += 1 print("Loaded!") return examples, len(examples)
def __getitem__(self, idx): """ returns the idx-image, its format is (C, H, W), RGB :param idx: the idx_th image. :return: """ id = self.ids[idx] annos = ET.parse( os.path.join(self.data_dir, 'Annotations', id + '.xml')) bboxs = [] labels = [] for obj in annos.findall('object'): pos = obj.find('bndbox') bboxs.append([ int(pos.find(tag).text) for tag in ['ymin', 'xmin', 'ymax', 'xmax'] ]) name = obj.find('name').text.lower().strip() labels.append(VOC_BBOX_LABEL_NAMES.index(name)) bboxs = np.asarray(bboxs).astype(np.float32) labels = np.stack(labels).astype(np.int32) img_file = os.path.join(self.data_dir, 'JPEGImages', idx + '.jpg') img = read_image(img_file) return img, bboxs, labels
def cb_image_in(self, msg): if self.active and not self.is_shutdown: if self.verbose: self.log('Received image.', 'info') if self.parametersChanged: self.log('Parameters changed.', 'info') self.refresh_parameters() self.parametersChanged = False img = utils.read_image(msg) if img is None: self.log('Got empty image msg.') height, width, depth = img.shape if self.rectify: img_temp = img.copy() self.pcm.rectifyImage(img_temp, img_temp) img = img_temp if self.verbose: utils.publish_image(self.bridge, self.pub_rectified, img) img_out = self.camera_img_to_birdseye(img) utils.publish_image(self.bridge, self.pub_image_out, img_out, msg.header)
def main(): ''' Evaluation function. ''' #Check if a trained model is present. assert e_cfg.TRAINED_MODEL_PRESENCE, "There is no trained model present for evaluation! If a model is already placed in the appropriate folder, please check the name of the model file." vgg = Model(resized_img_size=e_cfg.RESIZED_IMAGE_SIZE, num_classes=e_cfg.NUM_CLASSES, init_weights=True) vgg = vgg.to(e_cfg.DEVICE) print("--- Model Architecture ---") print(vgg) #loads the model if a saved model. model_params = torch.load(e_cfg.MODEL_PATH+e_cfg.MODEL_NAME) #get vgg.load_state_dict(model_params) vgg.eval() #change the model to eval mode after loading the parameters. IMPORTANT STEP! print("Model parameters are loaded from the saved file!") in_img = input("Please input the path of the image you wish to be evaluated: ") loaded_image = read_image(image_path=in_img, resized_image_size=e_cfg.RESIZED_IMAGE_SIZE) #load the image using cv2. tensor_image = ToTensor(mode='eval')({'image':loaded_image})['image'] #convert the loaded numpy image to Tensor using eval mode and extract only the image from the dict. #adds an extra dimension to emulate the batch size of 1 in the front and move the tensor to GPU if available. tensor_image = tensor_image.view(1, tensor_image.size()[0], tensor_image.size()[1], tensor_image.size()[2]).to(e_cfg.DEVICE) prediction_tensor = vgg(tensor_image) #output from the network. predicted_class = evaluate_class(net_output=prediction_tensor, classes_list=e_cfg.CLASSES) #get the predicted class. print(predicted_class)
def test_im(im_num_start, im_num_end): # after training this function will predict the values on test images in a specific range global train_images global train_labels global test_images # get trained values p_list, probs = training.training(train_labels,train_images) test_result = [] # test_result will store the prediction of test images for i in xrange(im_num_start, im_num_end + 1): im = utils.read_image(i,test_images) prob_v = [0,0,0,0,0,0,0,0,0,0] # prob_v stored the prob. for each value vj for j in xrange(10): p_a_v = 0 for k in xrange(28): for l in xrange(28): # work with sums of log probabilities rather than products of probabilities to avoid underflow errors if im[k][l] == 0: p_a_v = p_a_v + math.log10(probs[j][k][l][0]) if p_a_v == 0: print 'problem' else: p_a_v = p_a_v + math.log10(probs[j][k][l][1]) if p_a_v == 0: print 'problem' prob_v[j] = math.log10(p_list[j]) + p_a_v predict = prob_v.index(max(prob_v)) test_result.append(predict) return test_result
def get_activation_pattern(self): '''Get full activation pattern of the whole layer''' # TODO: To complete and test this function. imgid_2_local_idx = {imgid: i for i, imgid in enumerate(self._curr_imgids)} organized_scores = [] scores_local_idx = [] novel_imgfns = [] for imgfn in self._curr_imgfn_2_imgid.keys(): im = utils.read_image(os.path.join(self._writedir, imgfn)) tim = self._transformer.preprocess('data', im) self._classifier.blobs['data'].data[...] = tim self._classifier.forward(end=self._net_layer) score = self._classifier.blobs[self._net_layer].data[0, :] if self._net_unit_x is not None: score = score[self._net_unit_x, self._net_unit_y] try: imgid = self._curr_imgfn_2_imgid[imgfn] local_idx = imgid_2_local_idx[imgid] organized_scores.append(score) scores_local_idx.append(local_idx) except KeyError: novel_imgfns.append(imgfn) return organized_scores, scores_local_idx, novel_imgfns
def spectrumToArrays(device, params, img_path, output_folder, file_name): g = tf.Graph() content_image = ut.read_image(img_path) with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = models.getModel(image, params) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array for i in range(len(content_image_y_val)): output_path = output_folder + layers_names[i] + "/" if not os.path.exists(output_path): os.makedirs(output_path) dirr = os.path.dirname(output_path + file_name) if not os.path.exists(dirr): os.makedirs(dirr) np.save(output_path + file_name, content_image_y_val[i]) #cleanUp del image del content_image del model del content_image_y_val del g
def get_catalog_data(self, compute_descriptors=True): iterator = self.catalog_images_paths if self.verbose: iterator = tqdm(iterator, desc="Get catalog data") self.catalog_data = { "keypoints": [], "descriptors": [], "labels": [], "shapes": [], } for catalog_path in iterator: for width in self.catalog_image_widths: img = utils.read_image(catalog_path, width=width) label = catalog_path.split("/")[-1][:-4] keypoints = utils.get_keypoints(img, self.catalog_keypoint_stride, self.keypoint_sizes) self.catalog_data["keypoints"] += list(keypoints) self.catalog_data["labels"] += [label] * len(keypoints) self.catalog_data["shapes"] += [img.shape[:2]] * len(keypoints) if compute_descriptors: descriptors = utils.get_descriptors( img, keypoints, self.feature_extractor) self.catalog_data["descriptors"] += list(descriptors) self.catalog_data["descriptors"] = np.array( self.catalog_data["descriptors"])
def seg_atlas(self, id_cmn): """ Apply atlas-based segmentation of `self.cmn_img[id_cmn]` using the list of CT images in `self.grp_img_data` and the corresponding segmentation masks in `self.grp_mask_data`. Return the resulting segmentation mask after majority voting. """ image = self.cmn_img[id_cmn] foreground_mask = image > 0 reg_masks = [] for id_grp in self.grp_img: # compute linear affine transformation with MI lin_trf = LinearTransform( im_ref=image, im_mov=self.grp_img[id_grp]) lin_xfm = lin_trf.est_transf(metric="MI", num_iter=200, fix_img_mask=foreground_mask) #, mov_img_mask=self.grp_mask[id_grp], fix_img_mask=self.cmn_mask[id_cmn] # apply it to group image and its mask lin_reg_img = sitk.Resample(self.grp_img[id_grp], image, lin_xfm, sitk.sitkLinear, 0.0, self.grp_img[id_grp].GetPixelID()) lin_reg_mask = sitk.Resample(self.grp_mask[id_grp], image, lin_xfm, sitk.sitkNearestNeighbor, 0.0, self.grp_mask[id_grp].GetPixelID()) # compute FFD transform nl_trf = NonLinearTransform( im_ref=image, im_mov=lin_reg_img) nl_xfm = nl_trf.est_transf(metric="SSD", num_iter=10 , fix_img_mask=foreground_mask)# fix_img_mask=self.cmn_mask[id_cmn] # apply it to linearly trasformed mask nl_reg_mask = sitk.Resample(lin_reg_mask, image,nl_xfm, sitk.sitkLinear, 0.0, lin_reg_mask.GetPixelID()) reg_masks.append(sitk.GetArrayFromImage(nl_reg_mask)) cmn_img = ut.read_image(CMN_IMG_PATH.format(id_cmn)) for mask in reg_masks: ut.plot_3d_img_masked(cmn_img, sitk.GetImageFromArray(mask)) return self.majority_voting(reg_masks)
def batch_works(k): if k == n_processes - 1: paths = all_paths[k * int(len(all_paths) / n_processes):] else: paths = all_paths[k * int(len(all_paths) / n_processes):(k + 1) * int(len(all_paths) / n_processes)] print(paths) for path in paths: o_path = os.path.join(output_path, os.path.basename(path)) if not os.path.exists(o_path): os.makedirs(o_path) x, y, z = perturb_patch_locations(base_locs, patch_size / 16) probs = generate_patch_probs(path, (x, y, z), patch_size, image_size) selections = np.random.choice(range(len(probs)), size=patches_per_image, replace=False, p=probs) image = read_image(path) print('image size: {}'.format(image.shape)) for num, sel in enumerate(selections): i, j, k = np.unravel_index(sel, (len(x), len(y), len(z))) patch = image[int(x[i] - patch_size / 2):int(x[i] + patch_size / 2), int(y[j] - patch_size / 2):int(y[j] + patch_size / 2), int(z[k] - patch_size / 2):int(z[k] + patch_size / 2), :] f = os.path.join(o_path, str(num)) np.save(f, patch) print('patch size: {}'.format(patch.shape))
def __getitem__(self, idx): if idx >= len(self.keys): idx = random.randint(0, len(self.keys) - 1) sample = self.samples[self.keys[idx]] image = read_image(sample.image_fp) mask = read_mask(sample.mask_fp) image = np.array(image) / 255.0 mask = np.array(mask) / 255.0 if self.phase == 'train': image, mask = train_augment(image, mask) else: image, mask = validation_augment(image, mask) image = np.float32(image) mask = np.float32(mask) image = self.to_tensor(image) image = self.normalize(image) mask = self.to_tensor(mask[:, :, None])[0] return image, mask
def predict_query(self, query_path, classifier=None, apply_threshold=True): # Read img query_img = utils.read_image(query_path, width=self.query_image_width) query_original_h, query_original_w = cv2.imread(query_path).shape[:2] # Get keypoints query_keypoints = utils.get_keypoints(query_img, self.query_keypoint_stride, self.keypoint_sizes) query_kpts_data = np.array( [utils.keypoint2data(kpt) for kpt in query_keypoints]) # Get descriptors if self.verbose: print("Query description...") query_descriptors = utils.get_descriptors(query_img, query_keypoints, self.feature_extractor) # Matching self.get_matches_results(query_kpts_data, query_descriptors, query_img.shape) # Get bboxes bboxes = self.get_raw_bboxes(query_kpts_data) bboxes = self.filter_bboxes(bboxes, query_img.shape) bboxes = self.merge_bboxes(bboxes, query_img.shape) if classifier is not None: bboxes = self.add_classifier_score(bboxes, query_img, classifier) if apply_threshold: bboxes = self.filter_bboxes_with_threshold(bboxes) bboxes = self.reshape_bboxes_original_size( bboxes, (query_original_h, query_original_w), query_img.shape[:2]) return bboxes
def _get_scores(self): imgid_2_local_idx = {imgid: i for i, imgid in enumerate(self._curr_imgids)} # mapping from keys='imgid' (img filenames) to value=i (local_idx, in [0,40) ) # i.e. the inverse mapping of `self._curr_imgids`. # Note the `local_idx` is the order number of the img in the order of `imgid` # And output scores are in the order of the `_curr_imgfn` organized_scores = [] # ? organized_pattern = [] scores_local_idx = [] # ? novel_imgfns = [] # ? for imgfn in self._curr_imgfn_2_imgid.keys(): im = utils.read_image(os.path.join(self._writedir, imgfn)) # shape=(83, 83, 3) tim = self._transformer.preprocess('data', im) # shape=(3, 227, 227) self._classifier.blobs['data'].data[...] = tim self._classifier.forward(end=self._net_layer) score = self._classifier.blobs[self._net_layer].data[0, self._net_iunit] if self.record_pattern: # record the whole layer's activation score_full = self._classifier.blobs[self._net_layer].data[0, :] # Use the `self._net_iunit` for indexing the output if self._net_unit_x is not None: # if `self._net_unit_x/y` are provided, then use them to slice the output score score = score[self._net_unit_x, self._net_unit_y] try: imgid = self._curr_imgfn_2_imgid[imgfn] local_idx = imgid_2_local_idx[imgid] organized_scores.append(score) scores_local_idx.append(local_idx) if self.record_pattern: organized_pattern.append(score_full) except KeyError: novel_imgfns.append(imgfn) # Record this `novel_imgfns` to report at the end of each gen. return organized_scores, scores_local_idx, novel_imgfns
def test_face_database(): fdb = FaceDatabase( "/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/tmp8", "LPQ") img = utils.read_image( "/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_02/3078.png" ) print "find_face returned: ", fdb.find_face(img)
def check_unprocessed_image(): image = utils.read_image( "/Users/matti/Documents/forritun/att_faces/10.bmp") fp = utils.FaceProcessor() image2 = fp.process_image(image) print "unprocessed: ", print_dimension(image2)
def search_matching_faces_callback(): data = request.get_json() if not verify_data(data, "image_data", "image_name", "bboxes"): resp = { "status_code": status_codes["insufficient_data"], "message": response_messages["insufficient_data"] } return jsonify(resp) if not verify_file_extension(data["image_name"]): resp = { "status_code": status_codes["file_extension_error"], "message": response_messages["file_extension_error"] } return jsonify(resp) temp_img_name = get_unique_id() + "." + data["image_name"].split(".")[-1] temp_img_path = os.path.join("temp", temp_img_name) decode_from_base64(data["image_data"], temp_img_path) img = read_image(temp_img_path) bboxes = data["bboxes"] token = get_unique_id() mp_progress_dict[token] = 0.0 proc = pool.apply_async(search_matching_faces, (img, bboxes, token, mp_progress_dict)) mp_result_dict[token] = proc resp = { "status_code": status_codes["success"], "message": response_messages["success"], "token": token } return jsonify(resp)
def detect_faces_callback(): data = request.get_json() if not verify_data(data, "image_data", "image_name"): resp = { "status_code": status_codes["insufficient_data"], "message": response_messages["insufficient_data"] } return jsonify(resp) if not verify_file_extension(data["image_name"]): resp = { "status_code": status_codes["file_extension_error"], "message": response_messages["file_extension_error"] } return jsonify(resp) temp_img_name = get_unique_id() + "." + data["image_name"].split(".")[-1] temp_img_path = os.path.join("temp", temp_img_name) decode_from_base64(data["image_data"], temp_img_path) img = read_image(temp_img_path) bboxes = detect_faces(img) resp = { "status_code": status_codes["success"], "message": response_messages["success"], "bboxes": bboxes } return jsonify(resp)
def merge_data_label(data_file, image_path, label_file=None): # 获取训练集和测试集的数据 data = pd.read_csv(data_file, sep='\t', header=None) file_list = data[0].values.tolist() image_data = np.zeros((len(file_list), 64, 64, 3)) print('data size is {}'.format(len(image_data.shape))) for i, x in enumerate(file_list): if (i % 50 == 0): print('{}th'.format(i)) path = os.path.join(image_path, x) print('image_path ', path) if os.path.isfile(path): image = read_image(path) image_data[i, :, :, :] = np.array(image) print('data size is {}'.format(i)) if label_file is not None: print('merge train data....') data.columns = ['file_name', 'label'] columns = pd.read_csv(ref.file_attribute, header=None, sep='\t')[1].values.tolist() train_label = pd.read_csv(label_file, sep='\t', header=None) train_label.columns = ['label'] + columns train_all_data = pd.merge(data, train_label, on='label', how='left') train_all_data.iloc[:, -30:].to_pickle('train_y.pkl') np.save('train_data.npy', image_data) else: np.save('test_data.npy', image_data)
def get_training_batch(self, batch_id, batch_size, image_size): start_id = batch_id * batch_size end_id = start_id + batch_size batch_images = [ read_image(self.image_paths[i], image_size) for i in range(start_id, end_id) ] batch_segmentations = [ read_segmentation(self.segmentation_paths[i], image_size, self.id2label) for i in range(start_id, end_id) ] # Data augmentation for i in range(batch_size): if random.random() > 0.5: # horizontal flip with probability 0.5 batch_images[i] = tf.reverse(batch_images[i], axis=[1]) batch_segmentations[i] = tf.reverse(batch_segmentations[i], axis=[1]) batch_images[i] = tf.image.random_brightness( batch_images[i], max_delta=0.08) # random brightness batch_images[i] = tf.image.random_contrast( batch_images[i], lower=0.95, upper=1.05) # random contrast x = tf.stack(batch_images, axis=0) # (batch_size, img_h, img_w, 3) y_true_labels = tf.stack(batch_segmentations, axis=0) # (batch_size, img_h, img_w) return x, y_true_labels
def check_histogram(): image = utils.read_image("/Users/matti/Documents/forritun/att_faces/matti2.jpg") utils.show_image_and_wait_for_input(image) fp = utils.FaceProcessor() image2 = fp.process_image(image) utils.show_image_and_wait_for_input(image2) utils.equalize_histogram(image2) utils.show_image_and_wait_for_input(image2)
def test_face(path, img, process=True): mynd = utils.read_image(os.path.join(path, img)) data = utils.image_to_base64png(mynd) url = "http://{}:{}/process/".format(host, port) if not process: url = "http://{}:{}/".format(host, port) response = requests.post(url, data={'face': base64.b64encode(data)}) print response.text
def process_single_image(input_file, destination_path): image = utils.read_image(input_file) image = utils.convert_single_file(image) if image is None: print "Error converting image" sys.exit(-1) filename = input_file[input_file.rfind("/")+1:] # hvað gerist ef það er engin slóð í path? filename = filename[:-4] + ".png" destination = os.path.join(destination_path, filename) CV_IMWRITE_PNG_COMPRESSION = 16 cv2.imwrite(destination, image, (CV_IMWRITE_PNG_COMPRESSION, 0))
def main(): content_image_path, style_image_path, max_steps, output_dir,\ content_weight, style_weight, tv_weight, output_image_name = utils.parseArgs() # clear previous output folders # if tf.gfile.Exists(output_dir): # tf.gfile.DeleteRecursively(output_dir) # tf.gfile.MakeDirs(output_dir) option_weights = [content_weight, style_weight, tv_weight] if tf.gfile.Exists(neural_config.train_dir): tf.gfile.DeleteRecursively(neural_config.train_dir) tf.gfile.MakeDirs(neural_config.train_dir) print "Read images..." content_image = utils.read_image(content_image_path) style_image = utils.read_image(style_image_path) content_feat_map = getContentValues(content_image, "Content1") style_grams = getStyleValues(style_image, "Style") build_graph(content_feat_map, style_grams, content_image, max_steps, output_dir, output_image_name, option_weights, "Gen")
def test_for_images(show_hist=False): IMG_PATH = abspath(join(dirname(__file__), pardir, 'images')) print "3. TEST KODOWANIA OBRAZÓW\n" images = listdir(IMG_PATH) for idx, image in enumerate(images, start=1): print " 3.%d. %s\n" % (idx, image) data = read_image(join(IMG_PATH, image)) diffed = differential_encoding(data) if show_hist: show_histogram(diffed, image) scaled = scale_to_positive(diffed) encode_and_print_stats(scaled, 8) print "\n=======================================================================\n"
("arora_01.jpg", -1) ] """ # threshold_lpq_normalized = threshold_function(0.67, 0.3) threshold_lpq_chisquared = threshold_function(70, 35) # threshold_spatial_cosine = threshold_function(0.908, 0.908) # threshold_spatial_chisuearbrd = threshold_function() # threshold = threshold_lpq_normalized threshold = threshold_lpq_chisquared # threshold = threshold_spatial_cosine for image, id in test_list: target_full_name = os.path.join(test_path, image) prufu_mynd = utils.read_image(target_full_name) # prufu_mynd = fp.process_image(utils.read_image(target_full_name)) if prufu_mynd is not None: res = model.predict(prufu_mynd) found_id = threshold(res) # result_from_res(res) print found_id, ",", id else: print "Gat ekki opnað prufumynd" """ p1 = fp.process_image(utils.read_image("/Users/matti/Documents/forritun/att_faces/arora_01.jpg")) p2 = utils.read_image("/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_to_search_for/arora_01.png") res1 = model.predict(p1) res2 = model.predict(p2) print res1
import tensorflow as tf import numpy as np from models import VGG16, I2V from utils import read_image, save_image, parseArgs, getModel, add_mean import argparse import time content_image_path, style_image_path, params_path, modeltype, width, alpha, beta, num_iters, device, optimize_simply, args = parseArgs() # The actual calculation print "Read images..." content_image = read_image(content_image_path, width) style_image = read_image(style_image_path, width) g = tf.Graph() with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = getModel(image, params_path, modeltype) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array print "Load style values..." image = tf.constant(style_image) model = getModel(image, params_path, modeltype) y = model.y() style_image_st_val = [] for l in range(len(y)): num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) style_image_st_val.append(sess.run(st)) # sess.run(st) is a constant numpy array
INITIAL_IMAGE_PATH = None CONTENT_LAYER = 'relu4_2' STYLE_LAYERS = {'relu1_1': 0.2, 'relu2_1': 0.2, 'relu3_1': 0.2, 'relu4_1': 0.2, 'relu5_1': 0.2} DEVICE = '/cpu:0' CONTENT_WEIGHT = 0.005 STYLE_WEIGHT = 1 DENOISE_WEIGHT = 1 ITERATIONS = 1000 # Network parameters VGG_19_PATH = 'models/imagenet-vgg-verydeep-19.mat' LEARNING_RATE = 10 POOLING_FUNCTION = 'MAX' # Load images content_image = utils.read_image(CONTENT_PATH) style_image = utils.read_image(STYLE_PATH) g = tf.Graph() with g.device(DEVICE), g.as_default(), tf.Session() as sess: # 1. Compute content representation print("1. Computing content representation...") content_shape = (1,) + content_image.shape # add batch size dimension x = tf.placeholder(tf.float32, content_shape) net, activations, img_mean = vgg.net(VGG_19_PATH, x, pooling_function=POOLING_FUNCTION) # Pre-process image content_image_pp = utils.preprocess_image(content_image, img_mean) content_representation = activations[CONTENT_LAYER].eval(feed_dict={x: np.array([content_image_pp])})
("3078.png", 41), ("8174.png", 41), ("40_1.png", 40), ("48_3.png", 48), ("51_4.png", 51), ("8_7.png", 8), ("0455.png", 57), ("kolla_01.png", -1), ("inga_01.png", -1) ] test_path = "/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_02" # test_path = "/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_to_search_for" test_faces = [] for image, id in test_list: test_faces.append((id, utils.read_image(os.path.join(test_path, image)))) # Then set up a handler for logging: handler = logging.StreamHandler(sys.stdout) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) # Add handler to facerec modules, so we see what's going on inside: logger = logging.getLogger("facerec") logger.addHandler(handler) logger.setLevel(logging.DEBUG) # ætla að prófa allar aðferðir # feature = Fisherfaces() m = (Fisherfaces(), PCA(), SpatialHistogram(), SpatialHistogram(LPQ())) classifiers = (
import tensorflow as tf import numpy as np from models import VGG16, I2V from utils import read_image, parseArgs, getModel, add_mean, sub_mean import argparse content_image_path, params_path, modeltype, maxfilters = parseArgs() print "Read images..." content_image_raw = read_image(content_image_path) content_image = sub_mean(content_image_raw) with tf.Graph().as_default(), tf.Session() as sess: print "Load content values..." image = tf.constant(content_image) model = getModel(image, params_path, modeltype) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array # Set up the summary writer (saving summaries is optional) # (do `tensorboard --logdir=/tmp/vgg-visualizer-logs` to view it) with tf.variable_scope("Input"): tf.image_summary("Input Image", content_image) for l, y in enumerate(content_image_y_val): print "Layer ", l, " : ", y.shape with tf.variable_scope("Layer_%d"%l): for i in range(y.shape[3]): if i >= maxfilters: break temp = np.zeros((1, y.shape[1], y.shape[2], 1)).astype(np.float32) temp[0,:,:,0] = y[0,:,:,i] tf.image_summary("Layer %d, Filter %d"%(l, i), tf.constant(temp, name="Layer_%d_Filter_%d"%(l, i)))
def test_draw(): img = utils.read_image("/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_to_search_for/arora_01.jpg") utils.crop_jaw(img) utils.show_image_and_wait_for_input(img)
def check_unprocessed_image(): image = utils.read_image("/Users/matti/Documents/forritun/att_faces/10.bmp") fp = utils.FaceProcessor() image2 = fp.process_image(image) print "unprocessed: ", print_dimension(image2)
def test_base64png(): img = utils.read_image("/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_02/3078.png") b64 = utils.image_to_base64png(img) print b64[:100] img2 = utils.base64png_to_image_color(b64) utils.show_image_and_wait_for_input(img2)
def test_face_database(): fdb = FaceDatabase("/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/tmp8", "LPQ") img = utils.read_image("/Users/matti/Dropbox/Skjöl/Meistaraverkefni/server/test_faces_02/3078.png") print "find_face returned: ", fdb.find_face(img)
def produce_art(content_image_path, style_image_path, model_path, model_type, width, alpha, beta, num_iters): # The actual calculation print "Read images..." content_image = read_image(content_image_path, width) style_image = read_image(style_image_path, width) g = tf.Graph() with g.device(device), g.as_default(), tf.Session(graph=g, config=tf.ConfigProto(allow_soft_placement=True)) as sess: print "Load content values..." image = tf.constant(content_image) model = getModel(image, model_path, model_type) content_image_y_val = [sess.run(y_l) for y_l in model.y()] # sess.run(y_l) is a constant numpy array print "Load style values..." image = tf.constant(style_image) model = getModel(image, model_path, model_type) y = model.y() style_image_st_val = [] for l in range(len(y)): num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) style_image_st_val.append(sess.run(st)) # sess.run(st) is a constant numpy array print "Construct graph..." # Start from white noise # gen_image = tf.Variable(tf.truncated_normal(content_image.shape, stddev=20), trainable=True, name='gen_image') # Start from the original image gen_image = tf.Variable(tf.constant(np.array(content_image, dtype=np.float32)), trainable=True, name='gen_image') model = getModel(gen_image, model_path, model_type) y = model.y() L_content = 0.0 L_style = 0.0 for l in range(len(y)): # Content loss L_content += model.alpha[l] * tf.nn.l2_loss(y[l] - content_image_y_val[l]) # Style loss num_filters = content_image_y_val[l].shape[3] st_shape = [-1, num_filters] st_ = tf.reshape(y[l], st_shape) st = tf.matmul(tf.transpose(st_), st_) N = np.prod(content_image_y_val[l].shape).astype(np.float32) L_style += model.beta[l] * tf.nn.l2_loss(st - style_image_st_val[l]) / N ** 2 / len(y) # The loss L = alpha * L_content + beta * L_style # The optimizer global_step = tf.Variable(0, trainable=False) learning_rate = tf.train.exponential_decay(learning_rate=2.0, global_step=global_step, decay_steps=100, decay_rate=0.94, staircase=True) train_step = tf.train.AdamOptimizer(learning_rate).minimize(L, global_step=global_step) # A more simple optimizer # train_step = tf.train.AdamOptimizer(learning_rate=2.0).minimize(L) # Set up the summary writer (saving summaries is optional) # (do `tensorboard --logdir=/tmp/na-logs` to view it) tf.scalar_summary("L_content", L_content) tf.scalar_summary("L_style", L_style) gen_image_addmean = tf.Variable(tf.constant(np.array(content_image, dtype=np.float32)), trainable=False) tf.image_summary("Generated image (TODO: add mean)", gen_image_addmean) summary_op = tf.merge_all_summaries() summary_writer = tf.train.SummaryWriter('/tmp/na-logs', graph_def=sess.graph_def) print "Start calculation..." # The optimizer has variables that require initialization as well sess.run(tf.initialize_all_variables()) for i in range(num_iters): if i % 10 == 0: gen_image_val = sess.run(gen_image) save_image(gen_image_val, i, out_dir) print "L_content, L_style:", sess.run(L_content), sess.run(L_style) # Increment summary sess.run(tf.assign(gen_image_addmean, add_mean(gen_image_val))) summary_str = sess.run(summary_op) summary_writer.add_summary(summary_str, i) print "Iter:", i sess.run(train_step)