def main(args):
    torch.manual_seed(1)

    # crop input image and ground truth and save on disk
    cropped_input_images_path = os.path.join(args.save_cropped, 'input_images')
    cropped_gt_images_path = os.path.join(args.save_cropped, 'gt_images')

    if args.crop_images:
        crop_and_save(args, cropped_input_images_path, cropped_gt_images_path)

    seg_dataset = SegmentationData(cropped_input_images_path, cropped_gt_images_path, args.n_classes, args.phase)
    train_loader = DataLoader(seg_dataset, shuffle=True, num_workers=4, batch_size=args.batch_size)

    model = FCN(args.n_classes)
    use_gpu = torch.cuda.is_available()
    num_gpu = list(range(torch.cuda.device_count()))
    if use_gpu :
        model = model.cuda()
        model = nn.DataParallel(model, device_ids=num_gpu)

    optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum, weight_decay=args.weight_decay)
    criterion = nn.BCEWithLogitsLoss()
    losses = []
    for epoch in range(args.n_epoch):
        for i, (image, segement_im) in enumerate(train_loader):
            image = image.float()
            images = Variable(image.cuda())
            labels = Variable(segement_im.cuda())

            optimizer.zero_grad()

            outputs = model(images)

            loss = criterion(outputs, labels.float())
            loss.backward()
            optimizer.step()

            # add loss to a list for plotting it later
            if i == 0:
                losses.append(loss)
            print("epoch{} iteration {} loss: {}".format(epoch, i, loss.data.item()))

            if epoch%5 == 0:
                pred = outputs.data.max(1)[1].cpu().numpy()[0]

                decoded = decode_segmap(pred)
                decoded = Image.fromarray(decoded)

                path = os.path.join(args.output_path, 'output_%d_%d.png' % (epoch, i))

                decoded.save(path)

    # plot loss
    plot(losses, args)

    # save model
    if not os.path.exists(args.model_path):
        os.makedirs(args.model_path)
    model_name = os.path.join(args.model_path, 'fcn.pt')
    torch.save(model, model_name)
Ejemplo n.º 2
0
def crop_images(csv, channel, resize, square):

    #./main.py crop_images  --csv ResultTable\ -\ 2\ wells\,\ 1\ fields\,\ thresh\ 160_with_Traces_full_curated.csv
    #./main.py crop_images  --csv ResultTable\ -\ 2\ wells\,\ 1\ fields\,\ thresh\ 160_with_Traces_full_curated.csv --resize 200
    df = utils.read_csv(csv)
    print('Found number of cells: %s' % df.shape[0])
    #image_dir = './images/Ron/'
    image_dir = './images/LFS images/'
    # Loop by image
    for image_filename in df.FileName.unique():
        cells_in_img = df.loc[df['FileName'] == image_filename]
        cell_ids = []
        # Set the channel number in the image filename to load
        s = list(image_filename)
        s[15] = str(channel)
        ch_image_filename = "".join(s)
        # Load image
        image = skimage.io.imread(image_dir + ch_image_filename)

        # Build labelled image
        labelled = np.zeros(image.shape)
        count = 1
        for row_index, row in cells_in_img.iterrows():
            cyto_px_ids = map(int, row.cyto_boundaries.split(
            ))  # get locations of this cell's boundries as a list of ints
            labelled[np.unravel_index(
                cyto_px_ids, labelled.shape,
                order='F')] = count  # set this cell in the labelled image
            cell_ids.append(row.CellID)
            count += 1

        save_location = './images/cropped_images/ch%s-' % channel
        utils.crop_and_save(image,
                            labelled,
                            save_location,
                            filenames=cell_ids,
                            square=square,
                            resize=resize)
    print('Saved number of cropped cells: %s' % df.shape[0])
def main(args):
    # crop input image and save on disk
    cropped_input_images_path = os.path.join(args.save_cropped,
                                             'input_images_test')
    crop_and_save(args, cropped_input_images_path)

    seg_dataset = SegmentationData(cropped_input_images_path, phase=args.phase)
    test_loader = DataLoader(seg_dataset, shuffle=False)

    # load model
    model = torch.load(args.model_path)

    # create temp folder for saving prediction for each cropped input images
    temp_name = 'temp'
    if not os.path.exists(os.path.join(args.output_path, temp_name)):
        os.makedirs(os.path.join(args.output_path, temp_name))

    for i, (image, im_name) in enumerate(test_loader):
        image = image.float()

        image = Variable(image.cuda())

        # predict
        output = model(image)
        pred = np.squeeze(output.data.max(1)[1].cpu().numpy(), axis=0)
        decoded_im = decode_segmap(pred)

        # save image
        output_name = im_name[0].split('/')[-1]

        output_name = os.path.join(args.output_path, temp_name, output_name)
        decoded_im = Image.fromarray(decoded_im)
        decoded_im.save(output_name)

    stitch_predicted(args)
    shutil.rmtree(os.path.join(args.output_path, 'temp'))
Ejemplo n.º 4
0
    seeds,nr_nuclei = mh.label(rmax) # nuclei count
    imagew = mh.cwatershed(-SEf, seeds)

    # Remove areas that aren't nuclei 
    SEn=np.logical_not(SEt)
    imagew[SEn] = 0
    #pylab.imshow(imagew.astype(np.int16)) # convert datatype from int64 to int16 to display labelled value in []

    #prepare names for cropped cells
    filenamesDAPI[idx]= filenamesDAPI[idx][14:38]
    filenames=range(nr_nuclei)
    for x in filenames:
       filenames[x]=filenamesDAPI[idx]+str(filenames[x])

    # crop and label images
    utils.crop_and_save(SE, imagew,'./cropped_and_resized/', filenames= filenames, square=True, resize=70, padding=5)
    utils.crop_and_save(SE, imagew,'./cropped_and_resized/', filenames= filenames, square=True, resize=70, padding=5)


# generating random cell ID for each cropped cells
cellID =[]
filenames=list(glob.glob('./cropped_and_resized/*'))
for imageNumber in range(len(filenames)): 
    cellID.append(str(uuid.uuid4()))
filename="file.csv" 
df = pd.DataFrame (cellID, columns=["CellID"]) # assign unique ID for each cell 
df.to_csv(filename, index=False)



# finding a list of pixels in cell boundries 
Ejemplo n.º 5
0
    imagen = np.logical_not(imaget)
    imagew[imagen] = 0
    #pylab.imshow(imagew.astype(np.int16)) # convert datatype from int64 to int16 to display labelled value in []
    #prepare names for cropped cells
    filenames[idx] = filenames[idx][31:-4]
    index = range(nr_nuclei)

    cellNames = []
    for x in range(nr_nuclei):
        cellNames.append(filenames[idx] + str(index[x]))

    # crop and label images
    utils.crop_and_save(image,
                        imagew,
                        './new cellline/cropped/',
                        filenames=cellNames,
                        square=True,
                        resize=70,
                        padding=5)

# generating random cell ID for each cropped cells
cellID = []
filenames = list(glob.glob('./new cellline/cropped/*'))
for imageNumber in range(len(filenames)):
    cellID.append(str(uuid.uuid4()))
csv = "fileNew.csv"
df = pd.DataFrame(cellID, columns=["CellID"])  # assign unique ID for each cell
df.to_csv(csv, index=False)

# finding a list of pixels in cell boundries
cell_boundaries_list = []