def _Run(self, parent, params, comm_title): datadir = parent.u_info.data_path input_files = glob.glob(os.path.join(params['Image Folder'], "*.jpg")) input_png = glob.glob(os.path.join(params['Image Folder'], "*.png")) input_tif = glob.glob(os.path.join(params['Image Folder'], "*.tif")) input_files.extend(input_png) input_files.extend(input_tif) if len(input_files) == 0: print('No images in the Image Folder.') return im = cv2.imread(input_files[0], cv2.IMREAD_UNCHANGED) print('Target file to check color type : ', input_files[0]) print('Image dimensions : ', im.shape) print('Image filetype : ', im.dtype) image_width = im.shape[1] image_height = im.shape[0] if not (im.dtype == "uint8" and len(im.shape) == 3 and input_tif == []) : tmpdir = os.path.join(datadir, "tmp", "DNN_test_images") if os.path.exists(tmpdir) : shutil.rmtree(tmpdir) os.mkdir(tmpdir) for input_file in input_files: im_col = cv2.imread(input_file) filename = os.path.basename(input_file) filename = filename.replace('.tif', '.png') converted_input_file = os.path.join( tmpdir, filename ) cv2.imwrite(converted_input_file, im_col) params['Image Folder'] = tmpdir print('Filetype of images was changed to RGB 8bit, and stored in ', tmpdir) comm = parent.u_info.exec_translate +' ' \ + ' --mode predict ' \ + ' --save_freq 0 ' \ + ' --input_dir ' + params['Image Folder'] + ' ' \ + ' --output_dir ' + params['Output Segmentation Folder'] + ' ' \ + ' --checkpoint ' + params['Model Folder'] + ' ' \ + ' --image_height ' + str(image_height) + ' ' \ + ' --image_width ' + str(image_width) try: print(comm) print('Start inference.') m.UnlockFolder(parent.u_info, params['Output Segmentation Folder']) # Only for shared folder/file s.call(comm.split()) m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return except s.CalledProcessError as e: print("Inference was not executed.") m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return
def Execute3D(self, w): ## ## Load image ## filestack = self.ObtainTarget() params = self.ObtainParamsBottomTable(self.obj_args, self.args) output_path = params['Output Folder'] if len(output_path) == 0: print('Output folder unspecified.') return False # numz = len(filestack) # size = cv2.imread(filestack[0], cv2.IMREAD_GRAYSCALE).shape check_attribute = m.imread(filestack[0], flags=cv2.IMREAD_GRAYSCALE) tsize = check_attribute.shape tdtype = check_attribute.dtype input_volume = np.zeros([tsize[0], tsize[1], numz], tdtype) print('Loading images ...') for zi, filename in enumerate(filestack): # input_volume[:, :, zi] = cv2.imread(filename, cv2.IMREAD_GRAYSCALE).astype(tdtype) input_volume[:, :, zi] = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE) ## ## 2D/3D filter application ## for i in range(w.count()): item = w.item(i) text = item.text() instance = item.data(Qt.UserRole) params = self.ObtainParamsFilter(instance.args) type = self.fi.get_type(text) cls = self.fi.get_class(text) if type == '2d': for zi in range(numz): input_image = input_volume[:, :, zi] output_image = cls.Filter(self, input_image, params) input_volume[:, :, zi] = output_image elif type == '3d': tmp = cls.Filter(self, input_volume, params) input_volume = tmp.astype(np.uint16) # Unlock Folder m.UnlockFolder(self.parent.u_info, output_path) # Save segmentation print('Saving images ...') for zi, filename in enumerate(filestack): output_name = os.path.basename(filename) savename = os.path.join(output_path, output_name) root, ext = os.path.splitext(savename) if ext == ".tif" or ext == ".tiff" or ext == ".TIF" or ext == ".TIFF": m.save_tif16(input_volume[:, :, zi], savename) elif ext == ".png" or ext == ".PNG": m.save_png16(input_volume[:, :, zi], savename) print('2D/3D filters were applied!') # Lock Folder m.LockFolder(self.parent.u_info, output_path)
def _Run(self, parent, params, comm_title): ## print('Start postprocesing.') print(params['Output Filetype']) data = np.load(params['Target Sementation File (npz)']) print('File contents :', data.files) segmentation = data['segmentation'] print('Segmentation image size: ', segmentation.shape) filetype = params['Output Filetype'] ## ## if (filetype == '8-bit color PNG') or (filetype == '8-bit color TIFF'): ids = np.max(segmentation) print('Max segmentation ID: ', ids) colormap = np.random.randint(255, size=(ids + 2, 3), dtype=np.uint64) colormap[0, :] = 0 ## m.UnlockFolder(parent.u_info, params['Output Segmentation Folder']) ## for idz in range(segmentation.shape[0]): image2d = segmentation[idz, :, :] print('image2d size: ', image2d.shape) if filetype == '16-bit gray scale TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tif16(image2d, filename) elif filetype == '8-bit gray scale TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tif8(image2d, filename) elif filetype == '16-bit gray scale PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_png16(image2d, filename) elif filetype == '8-bit gray scale PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_png8(image2d, filename) elif filetype == '8-bit color PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_pngc(image2d, filename, colormap) elif filetype == '8-bit color TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tifc(image2d, filename, colormap) else: print('Data was not saved.') ## print(comm_title, 'was finished.') flag = m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return flag
def _UpdateFileSystem(self, dir_dojo): # Release m.UnlockFolder(self.u_info, dir_dojo) # Lock again m.LockFolder(self.u_info, dir_dojo) # Filetype self.u_info.open_files_type[dir_dojo] = 'Dojo' # Dropdown menu update self.parent.UpdateOpenFileMenu() # Combo box update SyncListQComboBoxExcludeDojoMtifManager.get().removeModel(dir_dojo) SyncListQComboBoxOnlyDojoManager.get().addModel(dir_dojo)
def _Run(self, parent, params, comm_title): # print('') training_image_file = os.path.join(params['FFNs Folder'], "grayscale_maps.h5") ground_truth_file = os.path.join(params['FFNs Folder'], "groundtruth.h5") record_file_path = os.path.join(params['FFNs Folder'], "tf_record_file") with h5py.File(training_image_file, 'r') as f: image = f['raw'][()] image_mean = np.mean(image).astype(np.int16) image_std = np.std(image).astype(np.int16) print('Training image mean: ', image_mean) print('Training image std : ', image_std) # #except: # print("Error: Training Image h5 was not loaded.") # return False # if params['Sparse Z'] != Qt.Unchecked: arg = '{"depth":9,"fov_size":[33,33,17],"deltas":[8,8,4]}' else: arg = '{"depth":12,"fov_size":[33,33,33],"deltas":[8,8,8]}' ## tmp = [ \ '--train_coords' , record_file_path , \ '--data_volumes' , 'validation1@' + training_image_file + '@raw' , \ '--label_volumes' , 'validation1@' + ground_truth_file + '@stack' , \ '--model_name' , 'convstack_3d.ConvStack3DFFNModel' , \ '--model_args' , arg , \ '--image_mean' , np.str( image_mean ) , \ '--image_stddev' , np.str( image_std ) , \ '--train_dir' , params['Model Folder (Empty/Model)'] , \ '--max_steps' , np.str(np.int(params['Max Training Steps'])) ] comm_train = parent.u_info.exec_train[:] comm_train.extend(tmp) # print(comm_title) print('') print(' '.join(comm_train)) print('') m.UnlockFolder(parent.u_info, params['Model Folder (Empty/Model)']) s.run(comm_train) m.LockFolder(parent.u_info, params['Model Folder (Empty/Model)']) print(comm_title, ' was finished.') # return True
def OpenFileFolder(self, fileName): #### Check open file status if fileName in self.u_info.open_files: return False if len(self.u_info.open_files) >= self.u_info.max_num_open_files: return False #### Check file/folder type filetype = self.CheckFileType(fileName) print('Filetype: ', filetype) if filetype == 'invalid': print('Invalid file type.') return False elif filetype == 'multiple type images': print('Folder contains multiple image types.') return False #### File open if os.path.isdir(fileName): lock_result = m.LockFolder(self.u_info, fileName) if lock_result == False: return False else: try: self.u_info.open_files4lock[fileName] = open(fileName, 'r+') except: print("Cannot open file.") return False self.u_info.open_files_type[fileName] = filetype self.u_info.open_files.insert(0, fileName) #### Dropdown menu updated self.UpdateOpenFileMenu() #### Manage open file history settings = QSettings('Trolltech', 'Recent Files Example') files = settings.value('recentFileList', []) try: files.remove(fileName) except ValueError: pass files.insert(0, fileName) del files[self.u_info.max_num_recent_files:] settings.setValue('recentFileList', files) self.UpdateRecentFileMenu()
def TerminateDojo(self): print("Asked tornado to exit\n") # Python3 self.u_info.worker_loop.stop() time.sleep(1) self.u_info.worker_loop.close() #self.u_info.worker_loop.stop() #self.u_info.worker_loop.call_soon_threadsafe(self.u_info.worker_loop.close) #self.u_info.dojo_thread.join() # if self.u_info.dojo_thread != None: m.LockFolder(self.u_info, self.u_info.files_path) self.u_info.dojo_thread = None self.u_info.files_found = False self.setWindowTitle(self.title) self.InitModeDojoMenu(self.dojo_icon_open_close)
def _Run(self, parent, params, comm_title): ## comm_run = self.u_info.exec_template + ' ' \ + ' --test_image_folder ' + params['Test image folder'] + ' ' \ + ' --inferred_segmentation_folder ' + params['Inferred segmentation folder'] + ' ' \ + ' --tensorflow_model_file ' + params['Tensorflow model file'] + ' ' print(comm_run) print('') ## m.UnlockFolder(self.u_info, params['Inferred segmentation folder'] ) # Only for shared folder/file s.run(comm_run.split()) m.LockFolder(self.u_info, params['Inferred segmentation folder']) print(comm_title, 'was finished.\n') ## return True
def _Run(self, parent, params, comm_title): ## tmp = [ '--test_image_folder' , params['Test image folder'] , \ '--inferred_segmentation_folder' , params['Inferred segmentation folder'] , \ '--tensorflow_model_file' , params['Model Folder'] ] comm_run = self.u_info.exec_template[:] comm_run.extend(tmp) print('') print(' '.join(comm_run)) print('') ## m.UnlockFolder(self.u_info, params['Inferred segmentation folder'] ) # Only for shared folder/file s.run(comm_run) m.LockFolder(self.u_info, params['Inferred segmentation folder']) print(comm_title, 'was finished.\n') ## return True
def Execute2D(self, w): ## ## Input files /Output folder ## self.filestack = self.ObtainTarget() params = self.ObtainParamsBottomTable(self.obj_args, self.args) output_path = params['Output Folder'] if len(output_path) == 0: print('Output folder unspecified.') return False # Unlock Folder m.UnlockFolder(self.parent.u_info, output_path) for filename in self.filestack: print(filename) output_name = os.path.basename(filename) # input_image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) input_image = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE) output_image = self.FilterApplication2D(w, input_image) output_dtype = output_image.dtype savename = os.path.join(output_path, output_name) root, ext = os.path.splitext(savename) if ext == ".tif" or ext == ".tiff" or ext == ".TIF" or ext == ".TIFF": if output_dtype == 'uint16': m.save_tif16(output_image, savename) elif output_dtype == 'uint8': m.save_tif8(output_image, savename) else: print('dtype mismatch: ', output_dtype) elif ext == ".png" or ext == ".PNG": if output_dtype == 'uint16': m.save_png16(output_image, savename) elif output_dtype == 'uint8': m.save_png8(output_image, savename) else: print('dtype mismatch: ', output_dtype) print('2D filters were applied!') # Lock Folder m.LockFolder(self.parent.u_info, output_path)
def ExecuteFolderOpen(self, folder_name, folder_type): ## Folder open lock_result = m.LockFolder(self.u_info, folder_name) if lock_result == False: return False self.u_info.open_files_type[folder_name] = folder_type self.u_info.open_files.insert(0, folder_name) ## Manage open file history settings = QSettings('Trolltech', 'Recent Files Example') recent_files = settings.value('recentFileList', []) try: recent_files.remove(folder_name) except ValueError: pass recent_files.insert(0, folder_name) del recent_files[self.u_info.max_num_recent_files:] settings.setValue('recentFileList', recent_files) self.UpdateRecentFileMenu()
def _Run(self, parent, params, comm_title): datadir = parent.u_info.data_path input_files = glob.glob(os.path.join(params['Image Folder'], "*.jpg")) input_png = glob.glob(os.path.join(params['Image Folder'], "*.png")) input_tif = glob.glob(os.path.join(params['Image Folder'], "*.tif")) input_files.extend(input_png) input_files.extend(input_tif) if len(input_files) == 0: print('No images in the Image Folder.') return im = cv2.imread(input_files[0], cv2.IMREAD_UNCHANGED) print('Target file to check color type : ', input_files[0]) print('Image dimensions : ', im.shape) print('Image filetype : ', im.dtype) image_size_x = im.shape[1] image_size_y = im.shape[0] converted_size_x = image_size_x converted_size_y = image_size_y std_sizes = [2**i for i in range(8, 15)] # 256, 512, ..., 16384 np_std_sizes = np.array(std_sizes) if (image_size_x > max(std_sizes) or image_size_y > max(std_sizes)): print('Image size is too big.') return if (image_size_x < min(std_sizes) or image_size_y < min(std_sizes)): print('Image size is too small.') return ## ## Check whether the target images should be converted. ## if not (im.dtype == "uint8" and len(im.shape) == 3 and input_tif == [] and image_size_x in std_sizes and image_size_y in std_sizes): # Generate tmpdir tmpdir = os.path.join(datadir, "tmp", "DNN_test_images") if os.path.exists(tmpdir): shutil.rmtree(tmpdir) os.mkdir(tmpdir) # Check image size converted_size_x_id = np.min( np.where((np_std_sizes - image_size_x) > 0)) converted_size_y_id = np.min( np.where((np_std_sizes - image_size_y) > 0)) converted_size_x = np_std_sizes[converted_size_x_id] converted_size_y = np_std_sizes[converted_size_y_id] fringe_size_x = converted_size_x - image_size_x fringe_size_y = converted_size_y - image_size_y # Image Conversion for input_file in input_files: im_col = cv2.imread(input_file) filename = path.basename(input_file) filename = filename.replace('.tif', '.png') converted_filename = os.path.join(tmpdir, filename) # add fringe X im_fringe_x = cv2.flip(im_col, 1) # flipcode > 0, left-right im_fringe_x = im_fringe_x[:, 0:fringe_size_x] converted_image = cv2.hconcat([im_col, im_fringe_x]) # add fringe Y im_fringe_y = cv2.flip(converted_image, 0) # flipcode = 0, top-bottom im_fringe_y = im_fringe_y[0:fringe_size_y, :] converted_image = cv2.vconcat([converted_image, im_fringe_y]) # Save cv2.imwrite(converted_filename, converted_image) #Complete params['Image Folder'] = tmpdir print('Filetype of images was changed to RGB 8bit, and stored in ', tmpdir) comm = parent.u_info.exec_translate +' ' \ + ' --mode predict ' \ + ' --save_freq 0 ' \ + ' --input_dir ' + params['Image Folder'] + ' ' \ + ' --output_dir ' + params['Output Segmentation Folder'] + ' ' \ + ' --checkpoint ' + params['Model Folder'] + ' ' \ + ' --image_height ' + str(converted_size_y) + ' ' \ + ' --image_width ' + str(converted_size_x) try: print(comm) print('Start inference.') m.UnlockFolder(parent.u_info, params['Output Segmentation Folder'] ) # Only for shared folder/file s.call(comm.split()) ## Cut out fringes output_files = glob.glob( os.path.join(params['Output Segmentation Folder'], "*.jpg")) output_png = glob.glob( os.path.join(params['Output Segmentation Folder'], "*.png")) output_tif = glob.glob( os.path.join(params['Output Segmentation Folder'], "*.tif")) output_files.extend(output_png) output_files.extend(output_tif) for output_file in output_files: im_col = cv2.imread(output_file) im_col = im_col[0:image_size_y, 0:image_size_x] cv2.imwrite(output_file, im_col) ## m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return except s.CalledProcessError as e: print("Inference was not executed.") m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return
def _Run(self, parent, params, comm_title): ## ## Remove preovious results. ## m.UnlockFolder(parent.u_info, params['FFNs Folder']) removal_file1 = os.path.join(params['FFNs Folder'], '0', '0', 'seg-0_0_0.npz') removal_file2 = os.path.join(params['FFNs Folder'], '0', '0', 'seg-0_0_0.prob') if os.path.isfile(removal_file1) or os.path.isfile(removal_file2): question = "Previous result of inference has been found in the FFNs Folder. Remove them?" reply = self.query_yes_no(question, default="yes") if reply == True: with contextlib.suppress(FileNotFoundError): os.remove(removal_file1) with contextlib.suppress(FileNotFoundError): os.remove(removal_file2) print('Inference files were removed.') else: print('FFN inference was canceled.') m.LockFolder(parent.u_info, params['FFNs Folder']) return ## ## h5 file (target image file) generation. ## target_image_file_h5 = os.path.join(params['FFNs Folder'], "grayscale_inf.h5") try: target_image_files = m.ObtainImageFiles( params['Target Image Folder']) images = [ m.imread(i, cv2.IMREAD_GRAYSCALE) for i in target_image_files ] images = np.array(images) image_z = images.shape[0] image_y = images.shape[1] image_x = images.shape[2] image_mean = np.mean(images).astype(np.int16) image_std = np.std(images).astype(np.int16) print('') print('x: {}, y: {}, z: {}'.format(image_x, image_y, image_z)) with h5py.File(target_image_file_h5, 'w') as f: f.create_dataset('raw', data=images, compression='gzip') print( '"grayscale_inf.h5" file (target inference image) was generated.' ) print('') except: print('') print("Error: Target Image h5 was not generated.") m.LockFolder(parent.u_info, params['FFNs Folder']) return False ## ## Tensorflow model extracted ## max_id_model = self.SelectMaxModel(params['Model Folder']) print('Tensorflow model : ', max_id_model) if max_id_model == False: print('Cannot find tensorflow model.') return False ## ## Inference configration file generation ## request = {} request['image'] = { "hdf5": "{}@raw".format(target_image_file_h5).replace('\\', '/') } request['image_mean'] = image_mean request['image_stddev'] = image_std request['checkpoint_interval'] = int(params['Checkpoint Interval']) request['seed_policy'] = "PolicyPeaks" request['model_checkpoint_path'] = max_id_model.replace('\\', '/') request['model_name'] = "convstack_3d.ConvStack3DFFNModel" if params['Sparse Z'] != Qt.Unchecked: request[ 'model_args'] = "{\\\"depth\\\": 9, \\\"fov_size\\\": [33, 33, 17], \\\"deltas\\\": [8, 8, 4]}" #request['model_args'] = ' {"depth":9,"fov_size":[33,33,17],"deltas":[8,8,4]} ' else: request[ 'model_args'] = "{\\\"depth\\\": 12, \\\"fov_size\\\": [33, 33, 33], \\\"deltas\\\": [8, 8, 8]}" #request['model_args'] = ' {"depth":12,"fov_size":[33,33,33],"deltas":[8,8,8]} ' request['segmentation_output_dir'] = params['FFNs Folder'].replace( '\\', '/') inference_options = {} inference_options['init_activation'] = 0.95 inference_options['pad_value'] = 0.05 inference_options['move_threshold'] = 0.9 inference_options['min_boundary_dist'] = {"x": 1, "y": 1, "z": 1} inference_options['segment_threshold'] = 0.6 inference_options['min_segment_size'] = 1000 request['inference_options'] = inference_options config_file = os.path.join(params['FFNs Folder'], "inference_params.pbtxt") with open(config_file, "w", encoding='utf-8') as f: self.write_call(f, request, "") print('') print('Configuration file was saved at :') print(config_file) print('') ## ## Inference start (I gave up the use of run_inference because of the augment parsing problem) ## m.mkdir_safe(os.path.join(params['FFNs Folder'], '0', '0')) ## comm_inference = parent.u_info.exec_run_inference[:] params = [ '--image_size_x', np.str(image_x), '--image_size_y', np.str(image_y), '--image_size_z', np.str(image_z), '--parameter_file', config_file ] comm_inference += params print(comm_title) # print(comm_inference) print('') s.run(comm_inference) print('') print(comm_title, 'was finished.') print('') return True