Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions deepprofiler/dataset/sampling.py
Original file line number Diff line number Diff line change
@@ -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()

Expand All @@ -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)
Expand All @@ -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":
Expand All @@ -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):
Expand All @@ -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)

Expand Down