cell-data-loader 0.0.4__tar.gz → 0.0.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: cell_data_loader
3
- Version: 0.0.4
3
+ Version: 0.0.6
4
4
  Summary: Converts general images of cells into formats and labels for deep learning pipelines
5
5
  Project-URL: Homepage, https://github.com/mleming/CellDataLoader
6
6
  Author-email: Matt Leming <mleming@mgh.harvard.edu>
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/python3
2
-
3
2
  from src.cell_data_loader import CellDataloader
4
3
 
5
4
  try:
@@ -10,7 +9,7 @@ except:
10
9
  except:
11
10
  raise Exception("Need valid tensorflow")
12
11
 
13
- import numpy
12
+ import numpy as np
14
13
  import os
15
14
 
16
15
  def example_numpy(verbose=True):
@@ -20,13 +19,13 @@ def example_numpy(verbose=True):
20
19
  the test/train folders in this script are the same -- you would need to
21
20
  have a separate train/test set in your own implementation
22
21
  """
23
- wd = os.path.dirname(os.path.realpath(__file__))
22
+ wd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
24
23
  imfolder1 = os.path.join(wd,'data',
25
24
  '3368914_4_non_tumor')
26
25
  imfolder2 = os.path.join(wd,'data',
27
26
  '4173633_5')
28
27
 
29
- model = resnet50()
28
+ #model = resnet50()
30
29
  model = tf.keras.applications.resnet50.ResNet50(
31
30
  include_top=True,
32
31
  weights='imagenet',
@@ -42,13 +41,13 @@ def example_numpy(verbose=True):
42
41
  dtype="numpy",verbose=False)
43
42
 
44
43
  z = None
45
- for epoch in range(100):
44
+ for epoch in range(1):
46
45
  for image,y in dataloader_train:
47
46
  if z is None:
48
47
  z_dim = list(y.shape)
49
48
  z_dim[1] = 1000 - z_dim[1]
50
49
  z = np.zeros(z_dim)
51
- y = np.concatenate(y,z,axis=1)
50
+ y = np.concatenate((y,z),axis=1)
52
51
  model.fit(image,y)
53
52
 
54
53
  # Test
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/python3
2
2
 
3
- from cell_data_loader import CellDataloader
3
+ from src.cell_data_loader import CellDataloader
4
4
  from torchvision.models import resnet50 #, ResNet50_Weights
5
5
  import torch
6
6
  import os
@@ -48,7 +48,7 @@ def example_torch(gpu_ids = None,verbose=True):
48
48
  dtype="torch",
49
49
  verbose=False, gpu_ids=gpu_ids)
50
50
  if verbose: print("Beginning training")
51
- n_epochs = 500
51
+ n_epochs = 1
52
52
  for epoch in range(n_epochs):
53
53
  l = 0
54
54
  c = 0
@@ -65,7 +65,7 @@ def example_torch(gpu_ids = None,verbose=True):
65
65
  if verbose:
66
66
  print(
67
67
  "Epoch {epoch:d}/{n_epochs:d}: loss: {loss:.5f}".format(
68
- epoch=epoch,n_epochs=e_epochs,loss=l/c)
68
+ epoch=epoch,n_epochs=n_epochs,loss=l/c)
69
69
  )
70
70
  # Test
71
71
 
@@ -3,7 +3,7 @@
3
3
  #git init
4
4
  git add README.md example_numpy.py example_torch.py src tests requirements.txt pyproject.toml
5
5
  #git rm src/cell_data_loader/slice_images_out.py src/cell_data_loader/cell_segmenter.py
6
- git commit -m "Updated example torch, added toml"
6
+ git commit -m "Added augmentation"
7
7
  git branch -M main
8
8
  #git remote add origin https://github.com/mleming/CellDataLoader.git
9
9
  git push -u origin main
@@ -2,7 +2,7 @@
2
2
 
3
3
  name = "cell_data_loader"
4
4
 
5
- version = "0.0.4"
5
+ version = "0.0.6"
6
6
 
7
7
  dependencies = [
8
8
  "numpy",
@@ -12,6 +12,7 @@ from scipy.ndimage.interpolation import map_coordinates
12
12
  from scipy.ndimage.filters import gaussian_filter
13
13
  from .base_dataset import BaseDataset
14
14
  import torch,torchvision
15
+ torchvision.disable_beta_transforms_warning()
15
16
  from .util import *
16
17
  import warnings
17
18
 
@@ -204,7 +205,8 @@ class CellDataloader():#BaseDataset):
204
205
  cell_box_filelist = None,
205
206
  n_channels = None,
206
207
  channels_first = True,
207
- match_labels=False):
208
+ match_labels=False,
209
+ normalize=True):
208
210
 
209
211
  self.verbose = verbose
210
212
  self.label_balance = label_balance
@@ -220,6 +222,21 @@ class CellDataloader():#BaseDataset):
220
222
  self.dtype = dtype
221
223
  self.n_channels = n_channels
222
224
  self.channels_first = channels_first
225
+ self.augment_image = augment_image
226
+ self.normalize = normalize
227
+ if self.augment_image and self.dtype == "torch":
228
+ import torchvision.transforms.v2 as transforms
229
+
230
+ self.augment = transforms.Compose([
231
+ transforms.RandomHorizontalFlip(0.5),
232
+ transforms.RandomVerticalFlip(0.5),
233
+ transforms.GaussianBlur(5),
234
+ transforms.ColorJitter(brightness=(0.5,1.5),
235
+ contrast=(1), saturation=(0.5,1.5), hue=(-0.1,0.1)),
236
+ transforms.RandomResizedCrop(self.dim, antialias=True)])#,
237
+ #transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
238
+ self.augment2 = transforms.Compose([
239
+ transforms.ElasticTransform()])
223
240
  """
224
241
  If true, outputs given labels equally. This may repeat images if the
225
242
  counts of each don't line up.
@@ -365,6 +382,11 @@ class CellDataloader():#BaseDataset):
365
382
  self.n_channels = sample.get_n_channels()
366
383
  else:
367
384
  self.n_channels = n_channels
385
+ if self.normalize and self.dtype == "torch":
386
+ from torchvision.transforms.v2 import Normalize
387
+ self.normalizer = Normalize(
388
+ tuple([0.5 for _ in range(self.n_channels)]),
389
+ tuple([0.5 for _ in range(self.n_channels)]))
368
390
  for image_object in self.image_objects:
369
391
  image_object.n_channels = self.n_channels
370
392
  if self.verbose:
@@ -457,6 +479,14 @@ class CellDataloader():#BaseDataset):
457
479
  self.im_index = 0
458
480
  self.index += 1
459
481
  if self.index >= len(self.image_objects): break
482
+ #if self.augment_image and self.dtype == "torch":
483
+ #imdim = im.size()
484
+ #print("imdim: %s" % str(imdim))
485
+ #im = torch.permute(im,[len(imdim)-1]+list(range(0,len(imdim)-1)))
486
+ #print("imdim: %s" % str(im.size()))
487
+ #im = self.augment(im)
488
+ #im = torch.permute(im,list(range(1,len(imdim))) + [0])
489
+ #print("imdim: %s"%str(im.size()))
460
490
  if self.return_labels():
461
491
  return im,label
462
492
  else:
@@ -492,6 +522,22 @@ class CellDataloader():#BaseDataset):
492
522
  b = torch.moveaxis(self.batch,-1,1)
493
523
  else:
494
524
  b = self.batch
525
+ if (self.augment_image and self.n_channels <= 3) or self.normalize:
526
+ if not self.channels_first:
527
+ b = torch.permute(b,[0,-1] + list(range(1,len(b.size())-1)))
528
+ #print("b.size(): %s" % str(b.size()))
529
+ if self.n_channels <= 3 and self.augment:
530
+ b = self.augment(b)
531
+ if self.dim[0] > 32 and self.dim[1] > 32:
532
+ b = self.augment2(b)
533
+ #print("b.size(): %s" % str(b.size()))
534
+ #print([0]+list(range(2,len(b.size()))) + [1])
535
+ if self.normalize:
536
+ b = self.normalizer(b)
537
+ if not self.channels_first:
538
+ b = torch.permute(b,[0]+list(range(2,len(b.size()))) + [1])
539
+ #print("b.size(): %s" % str(b.size()))
540
+ #print("sss")
495
541
  elif self.dtype == "numpy":
496
542
  if len(im.shape) == 2 and self.n_channels == 1:
497
543
  im = np.expand_dims(im,axis=2)
@@ -8,7 +8,7 @@ import numpy as np
8
8
  import torch
9
9
 
10
10
  wd = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
11
- im_root = os.path.join(wd,'data')
11
+ im_root = os.path.join(os.path.dirname(wd),'data')
12
12
  #imfile_svs = os.path.join(im_root,'10447627_1.svs')
13
13
  imfolder1 = os.path.join(im_root,'4173633_5')
14
14
  imfolder2 = os.path.join(im_root,'3368914_4_non_tumor')