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 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) savename = os.path.join(output_path, output_name) flag = m.SaveImage(output_image, savename) print('2D filters were applied!') # Change folder type self.parent.parent.ExecuteCloseFileFolder(output_path) self.parent.parent.OpenFolder(output_path)
def _ChangeZ(): self.filestack = self.ObtainTarget() # print("self.filestack: ", self.filestack) #if len(self.filestack) == 0 : # print("No image files") # return False sz = self.control_thumbnail[0].value() # Z 0:99 if len(self.filestack) > 0: znum = len(self.filestack) id = np.floor(znum * sz / MAXSLIDER).astype(np.uint16) #self.target_image = cv2.imread(self.filestack[id], cv2.IMREAD_GRAYSCALE).astype(np.uint8) self.target_image = m.imread(self.filestack[id], flags=cv2.IMREAD_GRAYSCALE, dtype=np.uint8) _ChangeXY()
def get_tile(self, file): ''' @override ''' super().get_tile(file) image_data = m.imread(file, cv2.IMREAD_GRAYSCALE) #if image_data.mode != "RGB": ##### # image_data = image_data.convert("RGB") ##### #print(image_data) content = cv2.imencode('.jpg', image_data, [cv2.IMWRITE_JPEG_QUALITY, 90])[1].tostring() content_type = 'image/jpeg' return content, content_type
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 get_volume(self, zoomlevel): ''' @override ''' files = super().get_volume(zoomlevel) out = None out_is_there = False # Sample all slices or a maximum number of z slices from all files for i in np.linspace( 0, len(files) - 1, num=min(len(files), self._Datasource__zSample_max)).astype('int'): print(files[i]) ################################################## input_image = m.imread(files[i], cv2.IMREAD_GRAYSCALE) if out_is_there: #out = np.dstack([out, input_image]) out = np.concatenate([out, input_image.flatten()]) else: #out = input_image out = input_image.flatten() out_is_there = True c_image_data = zlib.compress(out) output = BytesIO() output.write(c_image_data) content = output.getvalue() content_type = 'application/octstream' return content, content_type
def _Run(self, parent, params, comm_title): input_files = m.ObtainImageFiles(params['Image Folder']) if len(input_files) == 0: print('No images in the Image Folder.') return False im = m.imread(input_files[0], cv2.IMREAD_UNCHANGED) root, ext_image = os.path.splitext(os.path.basename(input_files[0])) print('') 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] if (image_size_x <= 256 or image_size_y <= 256): print('Image size is too small.') return False # Generate tmpdir tmpdir_standardized = os.path.join( params['Output Segmentation Folder (Empty)'], "standardized" + str(threading.get_ident()).zfill(6)[-6:]) if os.path.exists(tmpdir_standardized): shutil.rmtree(tmpdir_standardized) os.mkdir(tmpdir_standardized) # tmpdir_output = os.path.join( params['Output Segmentation Folder (Empty)'], "output" + str(threading.get_ident()).zfill(6)[-6:]) if os.path.exists(tmpdir_output): shutil.rmtree(tmpdir_output) os.mkdir(tmpdir_output) ## Check image size max_image_size = params['Maximal unit image size'] if max_image_size == '512': std_sizes = np.array([512]) elif max_image_size == '1024': std_sizes = np.array([512, 1024]) elif max_image_size == '2048': std_sizes = np.array([512, 1024, 2048]) else: print('Internal error at Maximal unit image size.') return False max_std_size = np.max(std_sizes) if image_size_x > max_std_size: unit_image_size_x = max_std_size num_tiles_x = np.int(np.ceil(float(image_size_x) / max_std_size)) else: unit_image_size_x = np.min(std_sizes[std_sizes >= image_size_x]) num_tiles_x = 1 if image_size_y > max_std_size: unit_image_size_y = max_std_size num_tiles_y = np.int(np.ceil(float(image_size_y) / max_std_size)) else: unit_image_size_y = np.min(std_sizes[std_sizes >= image_size_y]) num_tiles_y = 1 # converted_size_x = unit_image_size_x * num_tiles_x converted_size_y = unit_image_size_y * num_tiles_y fringe_size_x = converted_size_x - image_size_x fringe_size_y = converted_size_y - image_size_y # # output_files = [] print('Image standardization: ') for input_file in input_files: im_col = m.imread(input_file) # im_col = self._ChangeIntoColor(im_col) filename = path.basename(input_file) print(filename + ' ') for ext in [ '.TIF', '.tif', '.TIFF', '.tiff', '.PNG', '.jpg', '.jpeg', '.JPG', '.JPEG' ]: filename = filename.replace(ext, '.png') output_files.append(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 if (num_tiles_x == 1) and (num_tiles_y == 1): converted_filename = os.path.join(tmpdir_standardized, filename) m.imwrite(converted_filename, converted_image) else: for iy in range(num_tiles_y): for ix in range(num_tiles_x): y0 = iy * unit_image_size_y y1 = y0 + unit_image_size_y x0 = ix * unit_image_size_x x1 = x0 + unit_image_size_x current_tile = converted_image[y0:y1, x0:x1] converted_filename = str(ix).zfill(3)[-3:] + '_' + str( iy).zfill(3)[-3:] + '_' + filename converted_filename = os.path.join( tmpdir_standardized, converted_filename) m.imwrite(converted_filename, current_tile) #Complete print('') print('Images were split and changed into RGB 8bit, and stored in ', tmpdir_standardized) print('') tmp = ['--mode' , 'predict' , \ '--save_freq' , '0' , \ '--input_dir' , tmpdir_standardized, \ '--output_dir' , tmpdir_output, \ '--checkpoint' , params['Model Folder'], \ '--image_height', str(unit_image_size_y), \ '--image_width' , str(unit_image_size_x)] comm = parent.u_info.exec_translate[:] comm.extend(tmp) print('') print(' '.join(comm)) print('') print('Start inference.') print('') m.UnlockFolder(parent.u_info, params['Output Segmentation Folder (Empty)'] ) # Only for shared folder/file s.run(comm) print('') print('Segmentation reconstruction: ') for output_file in output_files: ## if (num_tiles_x == 1) and (num_tiles_y == 1): ## Remove fringes filename = os.path.join(tmpdir_output, output_file) inferred_segmentation = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE, dtype='uint8') else: ## Merge split images. inferred_segmentation = np.zeros( (converted_size_y, converted_size_x), dtype='uint8') for iy in range(num_tiles_y): for ix in range(num_tiles_x): y0 = iy * unit_image_size_y y1 = y0 + unit_image_size_y x0 = ix * unit_image_size_x x1 = x0 + unit_image_size_x current_tile_filename = str(ix).zfill( 3)[-3:] + '_' + str(iy).zfill( 3)[-3:] + '_' + output_file current_tile_filename = os.path.join( tmpdir_output, current_tile_filename) current_tile = m.imread(current_tile_filename, flags=cv2.IMREAD_GRAYSCALE, dtype='uint8') inferred_segmentation[y0:y1, x0:x1] = current_tile[:, :] inferred_segmentation = inferred_segmentation[0:image_size_y, 0:image_size_x] print('inferred_segmentation: ', inferred_segmentation.shape, inferred_segmentation.dtype) ## Save filename_base = os.path.splitext(os.path.basename(output_file))[0] filename_base = os.path.join( params['Output Segmentation Folder (Empty)'], filename_base) filetype = params['Output Filetype'] if filetype == '8-bit gray scale PNG': filename = filename_base + '.png' m.save_png8(inferred_segmentation, filename) elif filetype == '8-bit gray scale TIFF (Uncompressed)': filename = filename_base + '.tif' m.save_tif8(inferred_segmentation, filename, compression=1) elif filetype == '8-bit gray scale TIFF (Compressed)': filename = filename_base + '.tif' m.save_tif8(inferred_segmentation, filename) else: print('Internel error: bad filetype.') print(filename) ## # rm tmpdir if os.path.exists(tmpdir_standardized): shutil.rmtree(tmpdir_standardized) if os.path.exists(tmpdir_output): shutil.rmtree(tmpdir_output) parent.parent.ExecuteCloseFileFolder( params['Output Segmentation Folder (Empty)']) parent.parent.OpenFolder(params['Output Segmentation Folder (Empty)']) print('') print('Finish inference.') print('') return True
def _Run(self, parent, params, comm_title): ## ## Transform bitdepth of EM images and segmentation in the target directory. ## Translate.py only accepts unit24 (RGB color). ## img_files = glob.glob(os.path.join(params['Image Folder'], "*.jpg")) img_png = glob.glob(os.path.join(params['Image Folder'], "*.png")) img_tif = glob.glob(os.path.join(params['Image Folder'], "*.tif")) img_files.extend(img_png) img_files.extend(img_tif) img_files = sorted(img_files) if len(img_files) == 0: print('No image file.') return False im = m.imread(img_files[0], cv2.IMREAD_UNCHANGED) print('') print('Number of images : ', len(img_files)) print('Image color type : ', img_files[0]) print('Image dimensions : ', im.shape) print('Image filetype : ', im.dtype) seg_files = glob.glob( os.path.join(params['Segmentation Folder'], "*.jpg")) seg_png = glob.glob( os.path.join(params['Segmentation Folder'], "*.png")) seg_tif = glob.glob( os.path.join(params['Segmentation Folder'], "*.tif")) seg_files.extend(seg_png) seg_files.extend(seg_tif) seg_files = sorted(seg_files) if len(seg_files) == 0: print('') print('No segmentation file.') print('Aborted.') return False sg = m.imread(seg_files[0], cv2.IMREAD_UNCHANGED) print('') print('Number of Segmentation images : ', len(seg_files)) print('Segmentation image dimensions : ', sg.shape) print('Segmentation filetype : ', sg.dtype) print('') if len(img_files) != len(seg_files): print( 'The number of images is not equal to that of segmenation images.' ) print('Aborted.') return False # Generate tmpdir tmpdir = os.path.join(params['Model Folder (Empty)'], "paired" + str(threading.get_ident()).zfill(6)[-6:]) # ) if os.path.exists(tmpdir): shutil.rmtree(tmpdir) os.mkdir(tmpdir) for img_file, seg_file in zip(img_files, seg_files): img = m.imread(img_file) seg = m.imread(seg_file) img = np.array(img, dtype=np.uint8) seg = np.array(seg, dtype=np.uint8) if len(img.shape) == 2: img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) elif img.shape[2] == 4: img = cv2.cvtColor(img, cv2.COLOR_BGRA2BGR) elif img.shape[2] != 3: print('File is broken: ', img_file) print('Aborted.') return False if len(seg.shape) == 2: seg = cv2.cvtColor(seg, cv2.COLOR_GRAY2BGR) elif seg.shape[2] == 4: seg = cv2.cvtColor(seg, cv2.COLOR_BGRA2BGR) elif seg.shape[2] != 3: print('File is broken: ', seg_file) print('Aborted.') return False paired = cv2.hconcat([img, seg]) tmpname = os.path.splitext(os.path.basename(img_file))[0] filename_paired = os.path.join(tmpdir, tmpname + '.png') m.imwrite(filename_paired, paired) print('Paired images (RGB 8bit) are stored in ', tmpdir) print('') # # Dialog to specify directory # aug = params['Augmentation'] if aug == "fliplr, flipud, transpose": augmentation = ['--fliplr', '--flipud', '--transpose'] elif aug == "fliplr, flipud": augmentation = ['--fliplr', '--flipud', '--no_transpose'] elif aug == "fliplr": augmentation = ['--fliplr', '--no_flipud', '--no_transpose'] elif aug == "flipud": augmentation = ['--no_fliplr', '--flipud', '--no_transpose'] elif aug == "None": augmentation = ['--no_fliplr', '--no_flipud', '--no_transpose'] else: print( "Internal error at Augumentation of PartDialogTrainingExecutor." ) self._Cancel() return False # # ' --model ' + params['Model'] + ' ' # # parent.u_info.exec_translate, \ tmp = ['--batch_size' , '4', \ '--mode' , 'train', \ '--input_dir' , tmpdir, \ '--output_dir' , params['Model Folder (Empty)'], \ '--loss' , params['Loss Function'], \ '--network' , params['Network'], \ '--max_epochs' , str( params['Maximal Epochs'] ), \ '--display_freq' , str( params['Display Frequency'] ), \ '--u_depth' , str( params['U depth'] ), \ '--n_res_blocks' , str( params['N res blocks'] ), \ '--n_highway_units', str( params['N highway units'] ), \ '--n_dense_blocks' , str( params['N dense blocks'] ), \ '--n_dense_layers' , str( params['N dense layers']) ] comm = parent.u_info.exec_translate[:] comm.extend(tmp) comm.extend(augmentation) print('') print(' '.join(comm)) print('') print('Start training.') print('') m.UnlockFolder(parent.u_info, params['Model Folder (Empty)']) s.run(comm) # rm tmpdir if os.path.exists(tmpdir): shutil.rmtree(tmpdir) ## m.LockFolder(parent.u_info, params['Model Folder (Empty)']) ## Tentatively commented out. parent.parent.ExecuteCloseFileFolder(params['Model Folder (Empty)']) parent.parent.OpenFolder(params['Model Folder (Empty)']) print('') print('Finish training.') print('') return True
def _Run(self, parent, params, comm_title): # print('') tmp = [ \ '--input_volume' , os.path.join(params['Empty Folder for FFNs'], "groundtruth.h5@stack"), \ '--output_volume' , os.path.join(params['Empty Folder for FFNs'], "af.h5@af"), \ '--thresholds' , '0.025,0.05,0.075,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9', \ '--lom_radius' , '24,24,24', \ '--min_size' , '10000'] comm_compute_partition = parent.u_info.exec_compute_partition[:] comm_compute_partition.extend(tmp) # # tmp = [ \ '--partition_volumes' , 'validation1@'+os.path.join(params['Empty Folder for FFNs'], "af.h5@af") , \ '--coordinate_output' , os.path.join(params['Empty Folder for FFNs'], "tf_record_file") , \ '--margin' , '24,24,24 '] comm_build_coordinates = parent.u_info.exec_build_coordinates[:] comm_build_coordinates.extend(tmp) ## # try: ## training_image_files = m.ObtainImageFiles( params['Training Image Folder']) images = [ m.imread(i, cv2.IMREAD_GRAYSCALE) for i in training_image_files ] images = np.array(images) with h5py.File( os.path.join(params['Empty Folder for FFNs'], "grayscale_maps.h5"), 'w') as f: f.create_dataset('raw', data=images, compression='gzip') print('"grayscale_maps.h5" file (training image) was generated.') ground_truth_files = m.ObtainImageFiles(params['Ground Truth Folder']) images = [ m.imread(i, cv2.IMREAD_UNCHANGED) for i in ground_truth_files ] images = np.array(images).astype(np.int32) with h5py.File( os.path.join(params['Empty Folder for FFNs'], "groundtruth.h5"), 'w') as f: f.create_dataset('stack', data=images, compression='gzip') print('"groundtruth.h5" file (ground truth) was generated.') ## #except: # print("Error: h5 files (ground truth) were not generated.") # return False ## print(comm_title) print('Start compute_partitions.') print(' '.join(comm_compute_partition)) print('') s.run(comm_compute_partition) print('') print('Start build_coordinates.') print(' '.join(comm_build_coordinates)) print('') s.run(comm_build_coordinates) print('') print(comm_title, 'is finished.') print('') parent.parent.ExecuteCloseFileFolder(params['Empty Folder for FFNs']) parent.parent.OpenFolder(params['Empty Folder for FFNs']) return True
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