text: 
  #copy of j25
  import numpy as np
  
  from collections import namedtuple
  from functools import partial
  
  
  from PIL import Image
  
  import data_transforms
  import data_iterators
  import pathfinder
  import utils
  import app
  
  import torch
  import torchvision
  import torch.optim as optim
  import torch.nn as nn
  from torch.nn import init
  import torch.nn.functional as F
  from torch.autograd import Variable
  import math
  
  restart_from_save = None
  rng = np.random.RandomState(42)
  
  # transformations
  p_transform = {'patch_size': (256, 256),
                 'channels': 3,
                 'n_labels': 17}
  
  
  p_augmentation = {
      'zoom_range': (1, 1),
      'rotation_range': (0, 0),
      'shear_range': (0, 0),
      'translation_range': (-1, 1),
      'do_flip': True,
      'allow_stretch': False,
      'rot90_values': [0, 1, 2, 3],
      'flip': [0, 1]
  }
  
  
  
  # mean and std values calculated with the slim data iterator
  mean = 0.512
  std = 0.311
  
  # data preparation function
  def data_prep_function_train(x, p_transform=p_transform, p_augmentation=p_augmentation, **kwargs):
      x = x.convert('RGB')
      x = np.array(x)
      x = np.swapaxes(x,0,2)
      x = x / 255.
      x -= mean
      x /= std
      x = x.astype(np.float32)
      pert_aug = dict((k,p_augmentation[k]) for k in ('zoom_range','rotation_range','shear_range','translation_range','do_flip','allow_stretch') if k in p_augmentation)
      x = data_transforms.perturb(x, pert_aug, p_transform['patch_size'], rng,n_channels=p_transform["channels"])
      losless_aug = dict((k,p_augmentation[k]) for k in ('rot90_values','flip') if k in p_augmentation)
      x = data_transforms.random_lossless(x, losless_aug, rng)
      return x
  
  def data_prep_function_valid(x, p_transform=p_transform, **kwargs):
      x = x.convert('RGB')
      x = np.array(x)
      x = np.swapaxes(x,0,2)
      x = x / 255.
      x -= mean
      x /= std
      x = x.astype(np.float32)
      return x
  
  def label_prep_function(x):
      #cut out the label
      return x
  
  
  # data iterators
  batch_size = 192
  nbatches_chunk = 1
  chunk_size = batch_size * nbatches_chunk
  
  folds = app.make_stratified_split(no_folds=10)
  #for checking if folds are equal over multiple config files
  for fold in folds:
      print sum(fold)
  train_ids = folds[0] + folds[1] + folds[2] + folds[3] + folds[4] + folds[6] + folds[7] + folds[8] + folds[9]
  valid_ids = folds[5]
  all_ids = folds[0] + folds[1] + folds[2] + folds[3] + folds[4] + folds[5] + folds[6] + folds[7] + folds[8] + folds[9]
  
  bad_ids = []
  
  train_ids = [x for x in train_ids if x not in bad_ids]
  valid_ids = [x for x in valid_ids if x not in bad_ids]
  
  test_ids = np.arange(40669)
  test2_ids = np.arange(20522)
  
  
  train_data_iterator = data_iterators.DataGenerator(dataset='train-jpg',
                                                      batch_size=chunk_size,
                                                      img_ids = train_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_train,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=True, random=True, infinite=True)
  
  feat_data_iterator = data_iterators.DataGenerator(dataset='train-jpg',
                                                      batch_size=chunk_size,
                                                      img_ids = all_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=True, infinite=False)
  
  valid_data_iterator = data_iterators.DataGenerator(dataset='train-jpg',
                                                      batch_size=chunk_size,
                                                      img_ids = valid_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=True, infinite=False)
  
  test_data_iterator = data_iterators.DataGenerator(dataset='test-jpg',
                                                      batch_size=chunk_size,
                                                      img_ids = test_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=False, infinite=False)
  
  test2_data_iterator = data_iterators.DataGenerator(dataset='test2-jpg',
                                                      batch_size=chunk_size,
                                                      img_ids = test2_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=False, infinite=False)
  
  import tta
  tta = tta.LosslessTTA(p_augmentation)
  tta_test_data_iterator = data_iterators.TTADataGenerator(dataset='test-jpg',
                                                      tta = tta,
                                                      duplicate_label = False,
                                                      img_ids = test_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=False, infinite=False)
  
  tta_test2_data_iterator = data_iterators.TTADataGenerator(dataset='test2-jpg',
                                                      tta = tta,
                                                      duplicate_label = False,
                                                      img_ids = test2_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=False, infinite=False)
  
  tta_valid_data_iterator = data_iterators.TTADataGenerator(dataset='train-jpg',
                                                      tta = tta,
                                                      duplicate_label = True,
                                                      batch_size=chunk_size,
                                                      img_ids = valid_ids,
                                                      p_transform=p_transform,
                                                      data_prep_fun = data_prep_function_valid,
                                                      label_prep_fun = label_prep_function,
                                                      rng=rng,
                                                      full_batch=False, random=True, infinite=False)
  
  nchunks_per_epoch = train_data_iterator.nsamples / chunk_size
  max_nchunks = nchunks_per_epoch * 40
  
  
  validate_every = int(0.5 * nchunks_per_epoch)
  save_every = int(10 * nchunks_per_epoch)
  
  learning_rate_schedule = {
      0: 1e-3,
      int(max_nchunks * 0.4): 5e-4,
      int(max_nchunks * 0.6): 2e-4,
      int(max_nchunks * 0.8): 1e-4,
      int(max_nchunks * 0.9): 5e-5
  }
  
  # model
  from collections import OrderedDict
  
  class Fire(nn.Module):
  
      def __init__(self, inplanes, squeeze_planes,
                   expand1x1_planes, expand3x3_planes):
          super(Fire, self).__init__()
          self.inplanes = inplanes
          self.squeeze = nn.Conv2d(inplanes, squeeze_planes, kernel_size=1)
          self.squeeze_activation = nn.ReLU(inplace=True)
          self.expand1x1 = nn.Conv2d(squeeze_planes, expand1x1_planes,
                                     kernel_size=1)
          self.expand1x1_activation = nn.ReLU(inplace=True)
          self.expand3x3 = nn.Conv2d(squeeze_planes, expand3x3_planes,
                                     kernel_size=3, padding=1)
          self.expand3x3_activation = nn.ReLU(inplace=True)
  
      def forward(self, x):
          x = self.squeeze_activation(self.squeeze(x))
          return torch.cat([
              self.expand1x1_activation(self.expand1x1(x)),
              self.expand3x3_activation(self.expand3x3(x))
          ], 1)
  
  
  class SqueezeNet(nn.Module):
  
      def __init__(self, version=1.0, num_classes=1000):
          super(SqueezeNet, self).__init__()
          if version not in [1.0, 1.1]:
              raise ValueError("Unsupported SqueezeNet version {version}:"
                               "1.0 or 1.1 expected".format(version=version))
          self.num_classes = num_classes
          if version == 1.0:
              self.features = nn.Sequential(
                  nn.Conv2d(3, 96, kernel_size=7, stride=2),
                  nn.ReLU(inplace=True),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(96, 16, 64, 64),
                  Fire(128, 16, 64, 64),
                  Fire(128, 32, 128, 128),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(256, 32, 128, 128),
                  Fire(256, 48, 192, 192),
                  Fire(384, 48, 192, 192),
                  Fire(384, 64, 256, 256),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(512, 64, 256, 256),
              )
          else:
              self.features = nn.Sequential(
                  nn.Conv2d(3, 64, kernel_size=3, stride=2),
                  nn.ReLU(inplace=True),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(64, 16, 64, 64),
                  Fire(128, 16, 64, 64),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(128, 32, 128, 128),
                  Fire(256, 32, 128, 128),
                  nn.MaxPool2d(kernel_size=3, stride=2, ceil_mode=True),
                  Fire(256, 48, 192, 192),
                  Fire(384, 48, 192, 192),
                  Fire(384, 64, 256, 256),
                  Fire(512, 64, 256, 256),
              )
          # Final convolution is initialized differently form the rest
          final_conv = nn.Conv2d(512, self.num_classes, kernel_size=1)
          self.classifier = nn.Sequential(
              nn.Dropout(p=0.5),
              final_conv,
              nn.ReLU(inplace=True),
              nn.AvgPool2d(13)
          )
  
          for m in self.modules():
              if isinstance(m, nn.Conv2d):
                  if m is final_conv:
                      init.normal(m.weight.data, mean=0.0, std=0.01)
                  else:
                      init.kaiming_uniform(m.weight.data)
                  if m.bias is not None:
                      m.bias.data.zero_()
  
  
      def forward(self, x):
          x = self.features(x)
          x = self.classifier(x)
          return x.view(x.size(0), x.size(1))
  
      def _forward_features(self, x):
          return self.features(x)
  
  
  def squeezenet1_0(pretrained=False, **kwargs):
      r"""SqueezeNet model architecture from the `"SqueezeNet: AlexNet-level
      accuracy with 50x fewer parameters and <0.5MB model size"
      <https://arxiv.org/abs/1602.07360>`_ paper.
      Args:
          pretrained (bool): If True, returns a model pre-trained on ImageNet
      """
      model = SqueezeNet(version=1.0, **kwargs)
      if pretrained:
          model.load_state_dict(torch.utils.model_zoo.load_url(torchvision.models.squeezenet.model_urls['squeezenet1_0']))
      return model
  
  
  def squeezenet1_1(pretrained=False, **kwargs):
      r"""SqueezeNet 1.1 model from the `official SqueezeNet repo
      <https://github.com/DeepScale/SqueezeNet/tree/master/SqueezeNet_v1.1>`_.
      SqueezeNet 1.1 has 2.4x less computation and slightly fewer parameters
      than SqueezeNet 1.0, without sacrificing accuracy.
      Args:
          pretrained (bool): If True, returns a model pre-trained on ImageNet
      """
      model = SqueezeNet(version=1.1, **kwargs)
      if pretrained:
          model.load_state_dict(torch.utils.model_zoo.load_url(torchvision.models.squeezenet.model_urls['squeezenet1_1']))
      return model
  
  
  
  class Net(nn.Module):
      def __init__(self, shape=(3,256,256)):
          super(Net, self).__init__()
          self.squeezenet = squeezenet1_1(pretrained=True)
          self.n_classes = p_transform["n_labels"]
          self.squeezenet.classifier = self.final_classifier()
  
  
      def final_classifier(self):
          final_conv = nn.Conv2d(512, self.n_classes, kernel_size=1)
          classifier = nn.Sequential(
              nn.Dropout(p=0.5),
              final_conv,
              nn.AvgPool2d(15)
          )
          return classifier
  
  
      def forward(self, x):
          x = self.squeezenet(x)
          return F.sigmoid(x)
  
  
  def build_model():
      net = Net()
      return namedtuple('Model', [ 'l_out'])( net )
  
  
  
  # loss
  class MultiLoss(torch.nn.modules.loss._Loss):
  
      def __init__(self, weight):
          super(MultiLoss, self).__init__()
          self.weight = weight
  
      def forward(self, input, target):
          torch.nn.modules.loss._assert_no_grad(target)
  
          weighted = (self.weight*target)*(input-target)**2 +(1-target)*(input-target)**2
  
          return torch.mean(weighted)
  
  
  def build_objective():
      return MultiLoss(4.0)
  
  def build_objective2():
      return MultiLoss(1.0)
  
  def score(gts, preds):
      return app.f2_score_arr(gts, preds)
  
  # updates
  def build_updates(model, learning_rate):
      return optim.Adam(model.parameters(), lr=learning_rate)