def get_list_of_patches(image_list, center_list, size): patches = [ get_patches(image, centers, size) for image, centers in izip(image_list, center_list) if centers ] if len(size) == 3 else [ np.stack(get_patches2_5d(image, centers, size)) for image, centers in izip(image_list, center_list) if centers ] return patches
def get_xy( image_list, label_names, batch_centers, size, fc_shape, nlabels, preload, datatype ): n_images = len(image_list) centers, idx = centers_and_idx(batch_centers, n_images) print(''.join([' '] * 15) + 'Loading x') x = filter(lambda z: z.any(), get_patches_list(image_list, centers, size, preload)) x = np.concatenate(x) print(''.join([' '] * 15) + '- Concatenation') x[idx] = x print(''.join([' '] * 15) + 'Loading y') y = [np.array([l[c] for c in lc]) for l, lc in izip(labels_generator(label_names), centers)] print(''.join([' '] * 15) + '- Concatenation') y = np.concatenate(y) y[idx] = y y_fc = [np.asarray(get_patches(l, lc, fc_shape)) for l, lc in izip(labels_generator(label_names), centers)] y_fc = np.concatenate(y_fc) y_fc[idx] = y_fc if nlabels <= 2: y = y.astype(dtype=np.bool) y_fc = y_fc.astype(dtype=np.bool) y = [ to_categorical(y, num_classes=nlabels), to_categorical(y_fc, num_classes=nlabels).reshape((len(y_fc), -1, nlabels)) ] return x.astype(dtype=datatype), y
def get_defo_patches(defos, centers, size=(5, 5, 5)): ds_xyz = [np.split(d, 3, axis=4) for d in defos] defo_patches = [ np.stack( [get_patches(np.squeeze(d), centers, size) for d in d_xyz], axis=1 ) for d_xyz in ds_xyz ] patches = np.stack(defo_patches, axis=1) return patches
def get_patches_list(list_of_image_names, centers_list, size): patch_list = [ np.stack( map( lambda image: get_patches(norm(np.asarray(load_nii(image).dataobj)), centers, size), image_names ), axis=1 ) for image_names, centers in zip(list_of_image_names, centers_list) if centers ] return patch_list
def get_patch_labels(label_names, list_of_centers, output_size, nlabels, verbose=False): if verbose: print('%s- Loading y' % ' '.join([''] * 12)) y = map( lambda (labels, centers): np.minimum( get_patches(labels, centers, output_size), nlabels - 1, dtype=np.int8 ), izip(labels_generator(label_names), list_of_centers) ) y = map( lambda y_i: to_categorical(y_i, num_classes=nlabels).reshape((len(y_i), -1, nlabels)), y ) return y
def get_xy( image_list, label_names, batch_centers, size, # fc_shape, nlabels, preload, split, binary, iseg, experimental, datatype, pred_size ): n_images = len(image_list) centers, idx = centers_and_idx(batch_centers, n_images) x = get_patches_list(image_list, centers, size, preload) x = np.concatenate(filter(lambda z: z.any(), x)).astype(dtype=datatype) x[idx] = x # y = [np.array([l[c] for c in lc]) for l, lc in izip(labels_generator(label_names), centers)] # y = np.concatenate(y) y = get_label_patches(label_names, centers, size) y = to_nparray(y) y[idx] = y if split: # print ('==============+++++++===================') if binary: y = [ center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(np.copy(y).astype(dtype=np.bool), num_classes=2).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], 2]), pred_size), center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(np.array(y ==1 ).astype(dtype=np.int8) + np.array(y ==4).astype(dtype=np.int8), num_classes=2).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], 2]), pred_size), center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(np.array(y ==1 ).astype(dtype=np.int8) , num_classes=2).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], 2]), pred_size) # center_crop_3d_cube_ignore_first_liast_dim( # keras.utils.to_categorical(y, # num_classes=nlabels).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], nlabels]), # pred_size) ] else: if iseg: # print ('++++++++++++++') vals = [0, 10, 150, 250] labels = len(vals) y_labels = [keras.utils.to_categorical(y == l, num_classes=2) for l in vals[1:]] y_cat = np.sum( map(lambda (lab, val): np.array(y == val, dtype=np.uint8) * lab, enumerate(vals)), axis=0 ) y_cat = [keras.utils.to_categorical(y_cat, num_classes=labels)] if experimental == 3: y_fc = [np.asarray(get_patches(l, lc, size, preload)) for l, lc in izip(labels_generator(label_names), centers)] y_fc = np.concatenate(y_fc) y_fc[idx] = y_fc y_fc_cat = np.sum( map(lambda (lab, val): (y_fc == val).astype(dtype=np.uint8) * lab, enumerate(vals)), axis=0 ) y_fc_cat = [keras.utils.to_categorical(y_fc_cat, num_classes=labels).reshape((len(y_fc), -1, 4))] y_cat = y_cat + y_fc_cat elif experimental > 1: y_cat *= 3 y = y_labels + y_cat else: y = [ center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(np.copy(y).astype(dtype=np.bool), num_classes=2).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], 2]), pred_size), center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(np.array(y > 0).astype(dtype=np.int8) + np.array(y > 1).astype(dtype=np.int8), num_classes=3).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], 3]), pred_size), center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(y, num_classes=nlabels).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], nlabels]), pred_size) ] else: # y = keras.utils.to_categorical(np.copy(y).astype(dtype=np.bool), num_classes=nlabels) y = center_crop_3d_cube_ignore_first_liast_dim( keras.utils.to_categorical(y,num_classes=nlabels).reshape([y.shape[0], y.shape[1], y.shape[2], y.shape[3], nlabels]), pred_size) # print ('******') y = np.squeeze(y) x = np.transpose(x, axes=[0, 2, 3, 4, 1]) # print (x.shape) # print (y.shape) # print ('+++++++') return x, y
def get_image_patches(image_list, centers, size, preload): patches = [get_patches(image, centers, size) for image in image_list] if preload\ else [get_patches(norm(load_nii(name).get_data()), centers, size) for name in image_list] return np.stack(patches, axis=1)
def get_label_patches(label_list, centers, size): label_patch = [] for i in range(len(label_list)): temp = get_patches(load_nii(label_list[i]).get_data(), centers[i], size) label_patch.append(np.array(temp)) return label_patch
def get_xy( image_list, label_names, batch_centers, size, fc_shape, nlabels, preload, split, iseg, experimental, datatype, verbose=True ): n_images = len(image_list) centers, idx = centers_and_idx(batch_centers, n_images) if verbose: print(''.join([' '] * 15) + 'Loading x') x = filter(lambda z: z.any(), get_patches_list(image_list, centers, size, preload)) x = np.concatenate(x) if verbose: print(''.join([' '] * 15) + '- Concatenation') x[idx] = x if verbose: print(''.join([' '] * 15) + 'Loading y') y = [np.array([l[c] for c in lc]) for l, lc in izip(labels_generator(label_names), centers)] if verbose: print(''.join([' '] * 15) + '- Concatenation') y = np.concatenate(y) y[idx] = y if split: if iseg: vals = [0, 10, 150, 250] labels = len(vals) y_labels = [keras.utils.to_categorical(y == l, num_classes=2) for l in vals[1:]] y_cat = np.sum( map(lambda (lab, val): np.array(y == val, dtype=np.uint8) * lab, enumerate(vals)), axis=0 ) y_cat = [keras.utils.to_categorical(y_cat, num_classes=labels)] if experimental >= 3: y_fc = [np.asarray(get_patches(l, lc, fc_shape)) for l, lc in izip(labels_generator(label_names), centers)] y_fc = np.concatenate(y_fc) y_fc[idx] = y_fc y_fc_cat = np.sum( map(lambda (lab, val): (y_fc == val).astype(dtype=np.uint8) * lab, enumerate(vals)), axis=0 ) y_fc_cat = [ keras.utils.to_categorical(y_fc_cat, num_classes=labels).reshape((len(y_fc), -1, labels)) ] y_cat = y_cat + y_fc_cat elif experimental > 1: y_cat *= 3 y = y_labels + y_cat else: if experimental == 1: y_fc = [np.asarray(get_patches(l, lc, fc_shape)) for l, lc in izip(labels_generator(label_names), centers)] y_fc = np.concatenate(y_fc) y_fc[idx] = y_fc if nlabels <= 2: y = y.astype(dtype=np.bool) y_fc = y_fc.astype(dtype=np.bool) y = [ keras.utils.to_categorical(y, num_classes=nlabels), keras.utils.to_categorical(y_fc, num_classes=nlabels).reshape((len(y_fc), -1, nlabels)) ] else: y = [ keras.utils.to_categorical( np.copy(y).astype(dtype=np.bool), num_classes=2 ), keras.utils.to_categorical( np.array(y > 0).astype(dtype=np.int8) + np.array(y > 1).astype(dtype=np.int8), num_classes=3 ), keras.utils.to_categorical( y, num_classes=nlabels ) ] else: y = keras.utils.to_categorical(np.copy(y).astype(dtype=np.bool), num_classes=2) return x.astype(dtype=datatype), y
def get_image_patches(image_list, centers, size): patches = np.stack( [np.array(get_patches(image, centers, size)) for image in image_list], axis=1, ) if len(size) == 3 else np.array([np.stack(get_patches2_5d(image, centers, size)) for image in image_list]) return patches
def get_xy( image_list, label_names, batch_centers, size, # fc_shape, nlabels, preload, split, iseg, experimental, datatype): n_images = len(image_list) centers, idx = centers_and_idx(batch_centers, n_images) x = get_patches_list(image_list, centers, size, preload) x = np.concatenate(filter(lambda z: z.any(), x)).astype(dtype=datatype) x[idx] = x y = [ np.array([l[c] for c in lc]) for l, lc in izip(labels_generator(label_names), centers) ] y = np.concatenate(y) y[idx] = y if split: if iseg: vals = [0, 10, 150, 250] labels = len(vals) y_labels = [ keras.utils.to_categorical(y == l, num_classes=2) for l in vals[1:] ] y_cat = np.sum(map( lambda (lab, val): np.array(y == val, dtype=np.uint8) * lab, enumerate(vals)), axis=0) y_cat = [keras.utils.to_categorical(y_cat, num_classes=labels)] if experimental == 3: y_fc = [ np.asarray(get_patches(l, lc, size, preload)) for l, lc in izip(labels_generator(label_names), centers) ] y_fc = np.concatenate(y_fc) y_fc[idx] = y_fc y_fc_cat = np.sum(map( lambda (lab, val): (y_fc == val).astype(dtype=np.uint8) * lab, enumerate(vals)), axis=0) y_fc_cat = [ keras.utils.to_categorical(y_fc_cat, num_classes=labels).reshape( (len(y_fc), -1, 4)) ] y_cat = y_cat + y_fc_cat elif experimental > 1: y_cat *= 3 y = y_labels + y_cat else: # y = [ # keras.utils.to_categorical( # np.copy(y).astype(dtype=np.bool), # num_classes=2 # ), # keras.utils.to_categorical( # np.array(y > 0).astype(dtype=np.int8) + np.array(y > 1).astype(dtype=np.int8), # num_classes=3 # ), # keras.utils.to_categorical( # y, # num_classes=nlabels # ) # ] # encoder = LabelEncoder() # y = encoder.fit_transform(y) y = keras.utils.to_categorical(y, num_classes=nlabels) else: y = keras.utils.to_categorical(np.copy(y).astype(dtype=np.bool), num_classes=2) # print (x.shape) # print (y.shape) # print (y) # print ('++++') return x, y
def transfer_learning( net_domain, net, data, train_image, train_labels, train_roi, train_centers, options ): c = color_codes() # Network hyperparameters epochs = options['epochs'] net_epochs = options['net_epochs'] patch_width = options['patch_width'] patch_size = (patch_width, patch_width, patch_width) batch_size = options['batch_size'] d_factor = options['down_factor'] # We prepare the layers for transfer learning net_domain_conv_layers = [l for l in net_domain.layers if 'conv' in l.name] net_conv_layers = sorted( [l for l in net.layers if 'conv' in l.name], cmp=lambda l1, l2: int(l1.name[7:]) - int(l2.name[7:]) ) # We freeze the convolutional layers for the final net for layer in net.layers: if not isinstance(layer, Dense): layer.trainable = False net_domain_params = np.sum([K.count_params(p) for p in set(net_domain.trainable_weights)]) # We start retraining. # First we retrain the convolutional so the tumor rois appear similar after convolution, and then we # retrain the classifier with the new convolutional weights. # Data loading centers = [tuple(center) for center in np.random.permutation(train_centers)[::d_factor]] print(c['c'] + '[' + strftime("%H:%M:%S") + '] ' + c['g'] + 'Preparing ' + c['b'] + 'net' + c['nc'] + c['g'] + ' data (' + c['b'] + '%d' % len(centers) + c['nc'] + c['g'] + ' samples)' + c['nc']) x = [get_patches(image, centers, patch_size) for image in train_image] x = np.stack(x, axis=1).astype(np.float32) y = np.array([train_labels[center] for center in centers]) y = [ keras.utils.to_categorical( np.copy(y).astype(dtype=np.bool), num_classes=2 ), keras.utils.to_categorical( np.array(y > 0).astype(dtype=np.int8) + np.array(y > 1).astype(dtype=np.int8), num_classes=3 ), keras.utils.to_categorical( y, num_classes=5 ) ] # We start retraining. # First we retrain the convolutional so the tumor rois appear similar after convolution, and then we # retrain the classifier with the new convolutional weights. print(c['c'] + '[' + strftime("%H:%M:%S") + '] ' + c['g'] + 'Training the models ' + c['nc'] + c['b'] + '(%d patches)' % len(centers) + c['nc']) conv_data = net_domain.predict(np.expand_dims(train_roi, axis=0), batch_size=1) for e in range(epochs): print(c['b'] + 'Epoch %d/%d ' % (e+1, epochs) + c['nc']) print(''.join([' ']*14) + c['g'] + c['b'] + 'Domain' + c['nc'] + c['g'] + ' net ' + c['nc'] + c['b'] + '(%d parameters)' % net_domain_params + c['nc']) net_domain.fit(np.expand_dims(data, axis=0), conv_data, epochs=1, batch_size=1) for layer in net.layers: if isinstance(layer, Dense): if layer.name in ['core', 'tumor', 'enhancing']: layer.trainable = False else: layer.trainable = True net.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy']) net_params = np.sum([K.count_params(p) for p in set(net.trainable_weights)]) print(''.join([' ']*14) + c['g'] + c['b'] + 'Original (dense)' + c['nc'] + c['g'] + ' net ' + c['nc'] + c['b'] + '(%d parameters)' % net_params + c['nc']) net.fit(x, y, epochs=net_epochs, batch_size=batch_size) for layer in net.layers: if isinstance(layer, Dense): if layer.name in ['core', 'tumor', 'enhancing']: layer.trainable = True else: layer.trainable = False net.compile(optimizer='adadelta', loss='categorical_crossentropy', metrics=['accuracy']) net_params = np.sum([K.count_params(p) for p in set(net.trainable_weights)]) print(''.join([' ']*14) + c['g'] + c['b'] + 'Original (out)' + c['nc'] + c['g'] + ' net ' + c['nc'] + c['b'] + '(%d parameters)' % net_params + c['nc']) net.fit(x, y, epochs=net_epochs, batch_size=batch_size) # We transfer the convolutional weights after retraining the net for l_new, l_orig in zip(net_domain_conv_layers, net_conv_layers): l_orig.set_weights(l_new.get_weights())