From 66321454c1308fc893f4c28fe66925fd7ff1b851 Mon Sep 17 00:00:00 2001 From: michaelbornholdt Date: Tue, 27 Jul 2021 11:36:33 -0400 Subject: [PATCH] Update folder structure --- deepprofiler/dataset/sampling.py | 39 +++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/deepprofiler/dataset/sampling.py b/deepprofiler/dataset/sampling.py index 010100c5..9122da69 100644 --- a/deepprofiler/dataset/sampling.py +++ b/deepprofiler/dataset/sampling.py @@ -1,23 +1,21 @@ -import numpy as np import pandas as pd import skimage.io import threading -import pickle import tqdm import os import tensorflow as tf -import tensorflow.keras as keras import deepprofiler.imaging.boxes import deepprofiler.imaging.cropping + class SingleCellSampler(deepprofiler.imaging.cropping.CropGenerator): def start(self, session): self.session = session # Define input data batches - with tf.variable_scope("train_inputs"): + with tf.compat.v1.variable_scope("train_inputs"): self.config["train"]["model"]["params"]["batch_size"] = self.config["train"]["validation"]["batch_size"] self.build_input_graph() @@ -27,19 +25,20 @@ def process_batch(self, batch): batch["locations"][i]["Target"] = batch["targets"][i][0] batch["locations"][i]["Class_Name"] = self.dset.targets[0].values[batch["targets"][i][0]] metadata = pd.concat(batch["locations"]) - cols = ["Key","Target","Nuclei_Location_Center_X","Nuclei_Location_Center_Y"] - seps = ["+","@","x",".png"] - metadata["Image_Name"] = "" + cols = ["Key", "Target", "Nuclei_Location_Center_X", "Nuclei_Location_Center_Y"] + seps = ["/", "@", "x", ".png"] + metadata["Image_Name"] = '' for c in range(len(cols)): - metadata["Image_Name"] += metadata[cols[c]].astype(str).str.replace("/","-") + seps[c] - + metadata["Image_Name"] += metadata[cols[c]].astype(str) + seps[c] + print(metadata["Image_Name"]) + boxes, box_ind, targets, masks = deepprofiler.imaging.boxes.prepare_boxes(batch, self.config) feed_dict = { - self.input_variables["image_ph"]:batch["images"], - self.input_variables["boxes_ph"]:boxes, - self.input_variables["box_ind_ph"]:box_ind, - self.input_variables["mask_ind_ph"]:masks + self.input_variables["image_ph"]: batch["images"], + self.input_variables["boxes_ph"]: boxes, + self.input_variables["box_ind_ph"]: box_ind, + self.input_variables["mask_ind_ph"]: masks } for i in range(len(targets)): tname = "target_" + str(i) @@ -50,19 +49,20 @@ def process_batch(self, batch): def start_session(): - configuration = tf.ConfigProto() + configuration = tf.compat.v1.ConfigProto() configuration.gpu_options.allow_growth = True - main_session = tf.Session(config=configuration) - keras.backend.set_session(main_session) + main_session = tf.compat.v1.Session(config=configuration) + tf.compat.v1.keras.backend.set_session(main_session) return main_session + def is_directory_empty(outdir): # Verify that the output directory is empty os.makedirs(outdir, exist_ok=True) files = os.listdir(outdir) if len(files) > 0: erase = "" - while(erase != "y" and erase != "n"): + while erase != "y" and erase != "n": erase = input("Delete " + str(len(files)) + " existing files in " + outdir + "? (y/n) ") print(erase) if erase == "n": @@ -74,6 +74,7 @@ def is_directory_empty(outdir): os.remove(os.path.join(outdir, f)) return True + def sample_dataset(config, dset): outdir = config["paths"]["single_cell_sample"] if not is_directory_empty(outdir): @@ -99,7 +100,9 @@ def sample_dataset(config, dset): if len(batch["keys"]) > 0: crops, metadata = cropper.process_batch(batch) for j in range(crops.shape[0]): - image = deepprofiler.imaging.cropping.unfold_channels(crops[j,:,:,:]) + image = deepprofiler.imaging.cropping.unfold_channels(crops[j, :, :, :]) + plate, well_site, rest = metadata.loc[j, "Image_Name"].split('/') + os.makedirs(os.path.join(outdir, plate, well_site), exist_ok=True) skimage.io.imsave(os.path.join(outdir, metadata.loc[j, "Image_Name"]), image) all_metadata.append(metadata)