def __init__(self, hidden_size=20, X=None, cached_weights_path=None): self.vectorized_sample_bernoulli = np.vectorize(RBM.sample_bernoulli) self.vectorized_bernoulli = np.vectorize(RBM.bernoulli) self.vectorized_sample_gaussian = np.vectorize(RBM.sample_gaussian) self.weight_learning_rate = .001 self.hidden_learning_rate = .001 self.visible_learning_rate = .001 self.momentum = 0.5 if cached_weights_path is not None: for model_param in ['weights', 'hidden_biases', 'visible_biases']: saved_param_path = os.path.join(cached_weights_path, '%s.p' % model_param) self.__dict__[model_param] = pickle.load( open(saved_param_path, 'rb')) utilities.save_image(self.visible_biases.reshape(28, 28), 'visible_biases') for i in range(self.weights.shape[1]): utilities.save_image(self.weights[:, i].reshape(28, 28), 'weights_%d' % i) else: self.hidden_size = hidden_size if X is not None: self.train(X)
def main(): if len(sys.argv) > 1: image_to_open = sys.argv[1] else: response = requests.get("https://picsum.photos/1080") image_to_open = BytesIO(response.content) image = np.float32(Image.open(image_to_open)) model = Inception() if LAYER_INDICES is None: optimizations_to_perform = random.randrange(MIN_OPERATIONS, MAX_OPERATIONS) layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER), optimizations_to_perform) else: layer_indices = LAYER_INDICES final_image = image deep_dream = DeepDream(model) for i, layer_index in enumerate(layer_indices): print("LAYER " + model.layer_names[layer_index] + ", " + str(i) + " out of " + str(len(layer_indices))) layer = model.layers[layer_index] final_image = deep_dream.recursively_optimize(layer=layer, image=final_image, iterations=ITERATIONS, step_size=STEP_SIZE, rescale_factor=RESCALE_FACTOR, levels=LEVELS, blend=BLEND) save_image(final_image, OUTPUT_IMAGE_NAME + ".jpeg")
def train(self, X, epochs=40): X = utilities.normalize(X) (num_examples, visible_size) = X.shape self.weights = RBM.generate_weight_vector( visible_size * self.hidden_size).reshape(visible_size, self.hidden_size) self.visible_biases = RBM.generate_weight_vector(visible_size).reshape( visible_size, 1) self.hidden_biases = np.zeros(self.hidden_size).reshape( 1, self.hidden_size) self.weights_err_history = [] self.visible_biases_err_history = [] self.hidden_biases_err_history = [] prev_updates = (0, 0, 0) start = time.time() for e in range(epochs): np.random.shuffle(X) print 'EPOCH %d' % (e + 1) for i in range(num_examples): if i % 1000 == 0: print 'Trained %d examples in %d s' % ( e * num_examples + i, time.time() - start) prev_updates = self.train_example(X[i], prev_updates) bucket_size = 1000 iterations = [ k * bucket_size for k in range((e + 1) * num_examples / bucket_size) ] utilities.save_scatter( iterations, utilities.bucket(self.hidden_biases_err_history, bucket_size), 'hidden_err') utilities.save_scatter( iterations, utilities.bucket(self.visible_biases_err_history, bucket_size), 'visible_err') utilities.save_scatter( iterations, utilities.bucket(self.weights_err_history, bucket_size), 'weights_err') utilities.save_image(self.visible_biases.reshape(28, 28), 'visible_biases') RBM.save_weights('weights', self.weights) RBM.save_weights('visible_biases', self.visible_biases) RBM.save_weights('hidden_biases', self.hidden_biases)
def crop_images_according_to_annotations(dir_annotations, dir_images, dir_out): list_files = utilities.get_list_of_files(dir_annotations) utilities.check_output_dir(dir_out) print("Cropping and saving {} images.".format(len(list_files))) for i in range(0, len(list_files)): imagename = list_files[i].replace(".txt", ".png") image = utilities.read_image(dir_images, imagename) annotation = utilities.read_anno(dir_annotations, list_files[i]) cropped_image = image[int(annotation[1]):int(annotation[3]), int(annotation[0]):int(annotation[2])] utilities.save_image(dir_out, imagename, cropped_image)
def main(): if len(sys.argv) > 1: dir_to_open_right, dir_to_open_left, dir_to_render_right, dir_to_render_left = sys.argv[ 1], sys.argv[2], sys.argv[3], sys.argv[4] else: response = requests.get("https://picsum.photos/1080") image_to_open = BytesIO(response.content) model = Inception() deep_dream = DeepDream(model) for image_to_open_right, image_to_open_left in zip( os.listdir(dir_to_open_right), os.listdir(dir_to_open_left)): image_right, image_left = np.float32( Image.open(os.path.join( dir_to_open_right, image_to_open_right))), np.float32( Image.open( os.path.join(dir_to_open_left, image_to_open_left))) if LAYER_INDICES is None: optimizations_to_perform = random.randrange( MIN_OPERATIONS, MAX_OPERATIONS) layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER), optimizations_to_perform) else: layer_indices = LAYER_INDICES right_image = image_right for i, layer_index in enumerate(layer_indices): print("LAYER " + model.layer_names[layer_index] + ", " + str(i) + " out of " + str(len(layer_indices))) layer = model.layers[layer_index] right_image, left_image = deep_dream.recursively_optimize( layer=layer, image=right_image, left_image=image_left, iterations=ITERATIONS, step_size=STEP_SIZE, rescale_factor=RESCALE_FACTOR, levels=LEVELS, blend=BLEND) save_image(right_image, dir_to_render_right + "/" + image_to_open_right + ".jpeg") save_image(left_image, dir_to_render_left + "/" + image_to_open_left + ".jpeg")
def extract_words(img, visual=0): lines = line_horizontal_projection(img) words = [] for idx, line in enumerate(lines): if visual: save_image(line, 'lines', f'line{idx}') line_words = word_vertical_projection(line) for w in line_words: # if len(words) == 585: # print(idx) words.append((w, line)) # words.extend(line_words) # breakpoint() if visual: for idx, word in enumerate(words): save_image(word[0], 'words', f'word{idx}') return words
def _recognize_face(self,frame, faces): if len(faces) == 0: self.face_recognized_callback("No-Face-Detected") return self.load_model() for img in faces: rnd = random.randint(0, 10000000) path_file = "./recognition/tmp/" + str(rnd) + ".pgm" util.save_image(path_file, img) # fix the size of the input image If needed sized_face = None input_image = self.read_matrix_from_file(path_file) width, height = cv2.cv.GetSize(cv2.cv.fromarray(input_image)) if height != HEIGHT_DB_FACES or width != WIDTH_DB_FACES: sized_face = cv2.cv.CreateImage((WIDTH_DB_FACES, HEIGHT_DB_FACES), 8, 1) cv2.cv.Resize(input_image, sized_face, interpolation=cv2.cv.CV_INTER_CUBIC) else: sized_face = input_image # actual face recognition EventLogger.info("Started prediction") predicted_label_lbph, conf_lbph = self.model_lbph.predict(sized_face) EventLogger.info('LBPH: Predicted: %(predicted)s Confidence : %(confidence )s ' % {"predicted": predicted_label_lbph, "confidence ":conf_lbph}) try: os.remove(path_file) except Exception as e: EventLogger.error(e) if conf_lbph <= 110: user = util.get_real_user( self.csvPath, predicted_label_lbph) self.face_recognized_callback(user) else: self.face_recognized_callback("Non-Index")
def run(mode, model_name, trt_path, img_path, patch_size, use_fp16, save_mode): if mode == "TRT" and trt_path == None: print("Please provide a valid trt engine path") return if mode == "TRT": output = trt_helper_upsampler_piterative_experiment(model_name, trt_path, img_path, patch_size, use_fp16=use_fp16) elif mode == "TORCH": output = helper_upsampler_piterative_experiment( model_name, img_path, patch_size) else: print("Invalid mode") return fp = 16 if use_fp16 else 32 print(output.max()) print(output.min()) if mode == "TORCH": fp = "Actual" file_name = img_path.split("/")[-1].split(".")[0] + "_" + str( fp) + "_output_x4" if save_mode == "npz": np.savez("output_images/" + file_name + ".npz", output) elif save_mode == "npy": np.save("output_images/" + file_name, output) elif save_mode == "img": output = torch.tensor(output).int() output_folder = "output_images" c, h, w = output.shape ut.save_image(output, output_folder, h, w, 4, output_file_name=file_name)
def main(): if len(sys.argv) > 1: dir_to_open, render_fold = sys.argv[1], sys.argv[2] else: response = requests.get("https://picsum.photos/1080") image_to_open = BytesIO(response.content) model = Inception() deep_dream = DeepDream(model) for file_in_dir in os.listdir(dir_to_open): image = np.float32(Image.open(os.path.join(dir_to_open, file_in_dir))) if LAYER_INDICES is None: optimizations_to_perform = random.randrange( MIN_OPERATIONS, MAX_OPERATIONS) layer_indices = random.sample(range(MIN_LAYER, MAX_LAYER), optimizations_to_perform) else: layer_indices = LAYER_INDICES print(20 * ">", layer_indices) final_image = image for i, layer_index in enumerate(layer_indices): print("LAYER " + model.layer_names[layer_index] + ", " + str(i) + " out of " + str(len(layer_indices))) layer = model.layers[layer_index] final_image = deep_dream.recursively_optimize( layer=layer, image=final_image, iterations=ITERATIONS, step_size=STEP_SIZE, rescale_factor=RESCALE_FACTOR, levels=LEVELS, blend=BLEND) save_image(final_image, render_fold + "/" + file_in_dir + "_dreamed.jpeg")
def trt_forward_chop_iterative( x, trt_engine_path=None, shave=10, min_size=1024, device="cuda", print_result=True, scale=4, use_fp16=False, ): """ Forward chopping in an iterative way Parameters ---------- x : tensor input image. model : nn.Module, optional SR model. The default is None. shave : int, optional patch shave value. The default is 10. min_size : int, optional total patch size (dimension x dimension) . The default is 1024. device : int, optional GPU or CPU. The default is 'cuda'. print_result : bool, optional print result or not. The default is True. Returns ------- output : tensor output image. total_time : float total execution time. total_crop_time : float total cropping time. total_shift_time : float total GPU to CPU shfiting time. total_clear_time : float total GPU clearing time. """ dim = int(math.sqrt(min_size)) # getting patch dimension b, c, h, w = x.size() # current image batch, channel, height, width device = device patch_count = 0 output = torch.tensor(np.zeros((b, c, h * 4, w * 4))).numpy() total_time = 0 total_crop_time = 0 total_shift_time = 0 total_clear_time = 0 extra = x.clone().detach() f = open(trt_engine_path, "rb") runtime = trt.Runtime(trt.Logger(trt.Logger.WARNING)) engine = runtime.deserialize_cuda_engine(f.read()) context = engine.create_execution_context() new_i_s = 0 stream = cuda.Stream() for i in range(0, h, dim - 2 * shave): new_j_s = 0 new_j_e = 0 for j in range(0, w, dim - 2 * shave): patch_count += 1 h_s, h_e = i, min(h, i + dim) # patch height start and end w_s, w_e = j, min(w, j + dim) # patch width start and end lr = x[:, :, h_s:h_e, w_s:w_e] ba, ch, ht, wt = lr.shape print('\nx: {}\n'.format(x)) print('\nlr: {}\n'.format(lr)) input_lr = torch.tensor(lr).int() output_folder = "output_images" file_name = "data/test7.jpg".split("/")[-1].split(".")[0] ut.save_image(input_lr[0].int(), output_folder, ht, wt, 4, output_file_name=file_name + f"input_{i}_{j}_x4") lr = lr.numpy() print(f"shape of lr:{lr.shape}") # EDSR processing start = time.time() # torch.cuda.synchronize() USE_FP16 = use_fp16 target_dtype = np.float16 if USE_FP16 else np.float32 ba, ch, ht, wt = lr.shape lr = np.ascontiguousarray(lr, dtype=np.float32) # need to set input and output precisions to FP16 to fully enable it p_output = np.empty([b, c, ht * scale, wt * scale], dtype=target_dtype) # allocate device memory d_input = cuda.mem_alloc(1 * lr.nbytes) d_output = cuda.mem_alloc(1 * p_output.nbytes) bindings = [int(d_input), int(d_output)] sr = predict(context, lr, d_input, stream, bindings, p_output, d_output) output_sr = torch.tensor(sr).int() output_folder = "output_images" file_name = "data/test7.jpg".split("/")[-1].split(".")[0] ut.save_image(output_sr[0], output_folder, ht, wt, 4, output_file_name=file_name + f"{i}_{j}_x4") # torch.cuda.synchronize() end = time.time() processing_time = end - start total_time += processing_time # new cropped patch's dimension (h and w) n_h_s, n_h_e, n_w_s, n_w_e = 0, 0, 0, 0 n_h_s = 0 if h_s == 0 else (shave * 4) n_h_e = ((h_e - h_s) * 4) if h_e == h else (((h_e - h_s) - shave) * 4) new_i_e = new_i_s + n_h_e - n_h_s n_w_s = 0 if w_s == 0 else (shave * 4) n_w_e = ((w_e - w_s) * 4) if w_e == w else (((w_e - w_s) - shave) * 4) new_j_e = new_j_e + n_w_e - n_w_s # corpping image in crop_start = time.time() sr_small = sr[:, :, n_h_s:n_h_e, n_w_s:n_w_e] crop_end = time.time() crop_time = crop_end - crop_start total_crop_time += crop_time output[:, :, new_i_s:new_i_e, new_j_s:new_j_e] = sr_small del sr_small clear_start = time.time() if device == "cuda": ut.clear_cuda(None, None) clear_end = time.time() clear_time = clear_end - clear_start total_clear_time += clear_time if w_e == w: break new_j_s = new_j_e new_i_s = new_i_e if h_e == h: break return output, total_time, total_crop_time, total_shift_time, total_clear_time
def to_numpy(tensor): return tensor.detach().cpu().numpy( ) if tensor.requires_grad else tensor.cpu().numpy() img_path = "test2.jpg" input_batch = ut.load_image(img_path).unsqueeze(0) print(input_batch.shape) print(type(input_batch)) ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(input_batch)} execution_time = ut.timer() ort_outs = ort_session.run(None, ort_inputs) execution_time = execution_time.toc() print(execution_time) output = ort_outs[0] print(output) print(output.shape) output = torch.tensor(output).int() output_folder = "." file_name = img_path.split("/")[-1].split(".")[0] ut.save_image(output[0], output_folder, 100, 100, 4, output_file_name=file_name + "_x4")
for i, img_path in enumerate(images): image = ut.read_image(img_path, "BGR2RGB") #grayscale # Do the image cropping and save to the output rect = an.get_rect(image) # Ruple with bounding rect coords cropped = image[rect[1]:rect[3],rect[0]:rect[2]] # Simple cropping # This is the resizing stuff. Here I am assuming we # will be decimating the image (reducing size), hence # the interpolation used is the AREA relation. if resize: inter = cv2.INTER_AREA # OpenCV const for such interpolation cropped = cv2.resize(cropped, (width, height), interpolation=inter) # Saving the processed file name = files.get_filename(img_path) + ".jpg" save_path = files.append_path(out_path, name) ut.save_image(save_path, ut.convert_spaces(cropped, "RGB2BGR")) # Progress bar stuff perc = (i+1)/len(images) bar = int(np.round(perc*size)) line = "Processing [" line += "="*bar + " "*(size-bar) line += "] {:d}%".format(int(np.round(perc*100))) ut.update_line(line) # Thins func will use carriage return print("\nFinished processing.") print(Style.RESET_ALL)
def _found_face(self, frame, faces): for img in faces: imgPath = self.userPath + "/" + str(self.index) + ".pgm" EventLogger.info("Save Image: " + imgPath) util.save_image(imgPath, img) self.index += 1
R = sitk.ImageRegistrationMethod() R.SetInitialTransform(sitk.TranslationTransform(fixed.GetDimension())) R.SetMetricAsMeanSquares() R.SetOptimizerAsRegularStepGradientDescent(4.0, .01, 500) R.SetInterpolator(sitk.sitkLinear) # R.AddCommand( sitk.sitkIterationEvent, lambda: command_iteration(R) ) outTx = R.Execute(fixed, moving) # print("-------") # print(outTx) # print("Optimizer stop condition: {0}".format(R.GetOptimizerStopConditionDescription())) # print(" Iteration: {0}".format(R.GetOptimizerIteration())) # print(" Metric value: {0}".format(R.GetMetricValue())) resampler = sitk.ResampleImageFilter() resampler.SetReferenceImage(fixed) resampler.SetInterpolator(sitk.sitkLinear) resampler.SetDefaultPixelValue(100) resampler.SetTransform(outTx) out = resampler.Execute(moving) simg1 = sitk.Cast(sitk.RescaleIntensity(fixed), sitk.sitkUInt8) simg2 = sitk.Cast(sitk.RescaleIntensity(out), sitk.sitkUInt8) cimg = sitk.Compose(simg1, simg2, simg1 // 2. + simg2 // 2.) # showImage( simg2, "ImageRegistration1 Composition" ) from utilities import save_image save_image(simg2, 'sigm2.png')
image = ut.read_image(img_path, "BGR2GRAY") # Check if image is in the lower standard deviation # (note that the mean should be 0 theoretically) if norm_stats[i, 0] < -stdevs[0] and norm_stats[i, 1] < -stdevs[1]: # Sharpened image image = ft.image_conv(image, unsharp_mask) # Match the histogram final = an.match_histogram(image, model_hist) # Save the image to disc name = files.get_filename(img_path) + ".jpg" save_path = files.append_path(out_path, name) ut.save_image(save_path, final) if arguments['--view']: # Plot the difference here fig = plt.figure(figsize=(12, 7)) ax1 = plt.subplot(1, 2, 1) ax1.set_title("Original Image") ax1.imshow(image, cmap="gray") ax2 = plt.subplot(1, 2, 2) ax2.set_title("Sharpened Image") ax2.imshow(final, cmap="gray") fig.tight_layout() plt.show() # Progress bar stuff perc = (i + 1) / len(images)
def upsample(model_name, img_path, dimension, shave, batch_size, scale, device): file_name = img_path.split("/")[-1].split(".")[0] if model_name == "RRDB": input_image = ut.npz_loader(img_path) c, h, w = input_image.shape patch_list = {} create_patch_list(patch_list, input_image, dimension, shave, scale, c, h, w) model = md.load_rrdb(device=device) model.eval() min_dim = min(dimension, h, w) if min_dim != dimension: print( "\nPatch dimension is greater than the input image's minimum dimension. Changing patch dimension to input image's minimum dimension... \n " ) dimension = min_dim output, _ = batch_forward_chop( patch_list, batch_size, c, h, w, dimension, shave, scale, model=model, device=device, ) output = output.int() output_folder = "output_images" ut.save_image(output, output_folder, h, w, scale, output_file_name=file_name + "_x4") elif model_name == "EDSR": input_image = ut.load_image(img_path) c, h, w = input_image.shape patch_list = {} create_patch_list(patch_list, input_image, dimension, shave, scale, c, h, w) model = md.load_edsr(device=device) model.eval() min_dim = min(dimension, h, w) if min_dim != dimension: print( "\nPatch dimension is greater than the input image's minimum dimension. Changing patch dimension to input image's minimum dimension... \n " ) dimension = min_dim output, _ = batch_forward_chop( patch_list, batch_size, c, h, w, dimension, shave, scale, model=model, device=device, ) # ============================================================================= # print(output) # ============================================================================= output = output.int() output_folder = "output_images" ut.save_image(output, output_folder, h, w, scale, output_file_name=file_name + "_x4") else: print("Unknown model...")
) else: image = F.interpolate( image, scale_factor=scale_factor, mode=mode, align_corners=True, recompute_scale_factor=False, ) image = image.numpy() image = image[0, :, :, :] return image from PIL import Image if __name__ == "__main__": img_path = 'data/diff_sizes/test2_4000.jpg' i1 = ut.load_image(img_path) total_image = 1 for i in range(total_image): print(i1.shape) c, h, w = i1.shape scale_factor = 0.25 img = t_interpolate(i1, mode='bicubic', scale_factor=scale_factor) print(img.shape) output = torch.tensor(img).int() output_folder = 'data/diff_sizes/' file_name = img_path.split("/")[-1].split("_")[0] + "_" + str(int(h*scale_factor)) ut.save_image(output, output_folder, int(h*scale_factor), int(w*scale_factor), 1, output_file_name=file_name, add_date=False) i1 = img
def to_numpy(tensor): return (tensor.detach().cpu().numpy() if tensor.requires_grad else tensor.cpu().numpy()) img_path = "data/slices/13.npz" input_batch = ut.npz_loader(img_path).unsqueeze(0) input_ = torch.tensor(input_batch).int() output_folder = "output_images" file_name = img_path.split("/")[-1].split(".")[0] ut.save_image(input_[0], output_folder, 30, 30, 4, output_file_name=file_name + "_input_x4") print(input_batch.shape) ort_inputs = {ort_session.get_inputs()[0].name: to_numpy(input_batch)} execution_time = ut.timer() ort_outs = ort_session.run(None, ort_inputs) execution_time = execution_time.toc() print(execution_time) output = ort_outs[0] print(output) print(output.shape)