Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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
Ejemplo n.º 5
0
 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)
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    def CloseFileFolder(self, activeAction):
        fileName = activeAction.data()

        if os.path.isdir(fileName):
            m.UnlockFolder(self.u_info, fileName)
        else:
            self.u_info.open_files4lock[fileName].close()
            del self.u_info.open_files4lock[fileName]

        self.u_info.open_files.remove(activeAction.data())
        self.UpdateOpenFileMenu()

        SyncListQComboBoxExcludeDojoMtifManager.get().removeModel(fileName)
        SyncListQComboBoxOnlyDojoManager.get().removeModel(fileName)
Ejemplo n.º 8
0
    def ExecuteCloseFileFolder(self, file_name):
        if os.path.isdir(file_name):
            m.UnlockFolder(self.u_info, file_name)
        else:
            self.u_info.open_files4lock[file_name].close()
            del self.u_info.open_files4lock[file_name]

        self.u_info.open_files.remove(file_name)
        self.UpdateOpenFileMenu()

        SyncListQComboBoxEmptyManager.get().removeModel(file_name)
        SyncListQComboBoxFFNsManager.get().removeModel(file_name)
        SyncListQComboBoxModelManager.get().removeModel(file_name)
        SyncListQComboBoxEmptyModelManager.get().removeModel(file_name)
        SyncListQComboBoxImageManager.get().removeModel(file_name)
        SyncListQComboBoxDojoManager.get().removeModel(file_name)
Ejemplo n.º 9
0
 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
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    def LaunchDojo(self):  # wxGlade: ControlPanel.<event_handler>

        # Unlock Folder
        m.UnlockFolder(self.u_info, self.u_info.files_path)

        # Python3
        self.u_info.port = self.u_info.port + 1
        print('Port Num: ', self.u_info.port)
        self.u_info.worker_loop = asyncio.new_event_loop()
        self.u_info.dojo_thread = threading.Thread(target=self.StartThreadDojo)
        self.u_info.dojo_thread.setDaemon(True)  # Stops if control-C
        self.u_info.dojo_thread.start()

        # self.DojoHTTP.SetURL(self.u_info.url)
        # self.DojoHTTP.SetLabel(self.u_info.url)
        # self.DojoHTTP.SetLabel('Please click here!')
        # self.panel_URL.Show()
        self.ActiveModeDojoMenu(self.dojo_icon_open_close)
        # time.sleep(10)
        self.u_info.url = 'http://' + self.u_info.ip + ':' + str(
            self.u_info.port) + '/dojo/'
        self.table_widget.addTab('dojo', 'Dojo',
                                 self.u_info.url)  # ID, Title, URL
Ejemplo n.º 13
0
    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
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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