Exemplo n.º 1
0
 def data_preprocess(self, input_image, normalization=None, num_channels=0):
     if type(input_image) != np.ndarray:
         input_image = np.stack(input_image, axis=0)
     if normalization == 'CT':
         image = mt.medical_normalization(input_image, input_copy=False)
     elif normalization == 'CT_chest':
         image = mt.medical_normalization(input_image,
                                          max_bound=1000,
                                          min_bound=-1000,
                                          pixel_mean=0.25,
                                          crop=False,
                                          input_copy=False)
     elif normalization == 'CT_lung':
         image = mt.medical_normalization(input_image,
                                          max_bound=400,
                                          min_bound=-1000,
                                          pixel_mean=0.25,
                                          crop=True,
                                          input_copy=False)
     elif normalization == 'image':
         image_max = (np.zeros(1, dtype=input_image.dtype) - 1)[0]
         #image_max = 65535
         image = input_image / float(image_max)
     else:
         image = input_image
     if num_channels > 0:
         image = np.expand_dims(image, axis=0)
     if num_channels > 1:
         image = image.repeat(num_channels, axis=0)
     image = torch.from_numpy(image).float()
     return image
 posbatchend = min(pbi + BATCH_SIZE, positive_train_num)
 for pti in range(pbi, posbatchend):
     data_index = positive_train_indices[pti]
     pfile = tpfiles[data_index]
     positive_data = np.load(pfile)
     nodule_diameter = 0
     if "positive_batch" not in dir():
         positive_batch = extract_volumes(
             positive_data, nodule_diameter=nodule_diameter)
     else:
         positive_batch = np.concatenate(
             (positive_batch,
              extract_volumes(positive_data,
                              nodule_diameter=nodule_diameter)),
             axis=0)
 positive_batch = mt.medical_normalization(positive_batch, MAX_BOUND,
                                           MIN_BOUND, PIXEL_MEAN, True)
 positive_label = np.zeros(shape=(positive_batch.shape[0], 2),
                           dtype=float)
 positive_label[:, 0] = 1
 predictions, accuracies = sess.run([out_fc2, correct_prediction], {
     volume_input: positive_batch,
     real_label: positive_label
 })
 if not AUGMENTATION:
     for a in range(len(accuracies)):
         data_index = positive_train_indices[pbi + a]
         pfile = tpfiles[data_index]
         if accuracies[a]:
             correct_output.write(pfile +
                                  " {}\n".format(predictions[a]))
         else:
Exemplo n.º 3
0
     batchend = min(bi + BATCH_SIZE, test_num)
     batch_size = batchend - bi
     train_data = np.zeros(
         (batch_size, REGION_SIZE, REGION_SIZE, REGION_SIZE), dtype=float)
     train_label = np.zeros((batch_size, 2), dtype=float)
     for ti in range(bi, batchend):
         file = test_files[ti]
         malignancy = lt.sample_malignancy(file)
         if malignancy >= 0:
             train_label[ti - bi][1 - malignancy] = 1
         else:
             print('unknown malignancy, skip')
             continue
         data = np.load(file)
         data_cropped = mt.crop(data,
                                (REGION_SIZE, REGION_SIZE, REGION_SIZE))
         train_data[ti - bi] = mt.medical_normalization(data_cropped,
                                                        MAX_BOUND,
                                                        MIN_BOUND,
                                                        PIXEL_MEAN,
                                                        NORM_CROP,
                                                        input_copy=False)
     accuracy, prediction = sess.run([batch_accuracy, prediction_fusion], {
         volume_input: train_data,
         real_label: train_label
     })
     total_accuracy += accuracy * batch_size
     predictions[bi:batchend] = prediction
 total_accuracy /= float(test_num)
 print("accuracy:{}".format(total_accuracy))
 #print("prediction:\n{}" .format(predictions))