def create_hex_float_image(pixels, path, zero_mean=False):
    fixed_pixels = []
    pixels = pixels * 256
    if zero_mean:
        pixels = pixels - np.mean(pixels)
    for i in pixels:
        print str(SSIM.float_to_hex(i)[0:10])
        fixed_pixels.append(str(SSIM.float_to_hex(i))[0:10])

    with open(path, 'w') as f:
        for i in fixed_pixels:
            f.write('{}\n'.format(i))
Exemple #2
0
def main():
    training_data, validation_data, testing_data = mnist_loader.load_data_wrapper(
    )
    noisy_testing_data, noisy_training_data = post_processing.create_two_noisy_data(
        testing_data, training_data, 80)

    count = [0] * 10
    running_average = [np.zeros((784, 1), dtype='float32')] * 10

    for i in testing_data:
        label = i[1]
        count[label] = count[label] + 1
        running_average[label] = running_average[label] + i[0]

    for index, i in enumerate(count):
        running_average[index] = running_average[index] / count[index]

    clean_cw_ssim_vals = np.zeros((len(testing_data)))
    clean_ssim_vals = np.zeros((len(testing_data)))
    for index, i in enumerate(testing_data):
        label = i[1]
        clean_cw_ssim_vals[index] = SSIM.CW_SSIM(
            i[0].ravel(), running_average[label].ravel())
        clean_ssim_vals[index] = SSIM.SSIM(
            256 * i[0], 256 * running_average[label]) / 100.0

    print 'Mean of clean cw ssim: {}'.format(np.mean(clean_cw_ssim_vals))
    print 'Mean of clean ssim: {}'.format(np.mean(clean_ssim_vals))
    print 'std dev of clean cw ssim: {}'.format(np.std(clean_cw_ssim_vals))
    print 'std dev of clean ssim: {}'.format(np.std(clean_ssim_vals))

    noisy_cw_ssim_vals = np.zeros(len(testing_data))
    noisy_ssim_vals = np.zeros(len(testing_data))
    for index, i in enumerate(noisy_testing_data):
        label = i[1]
        noisy_cw_ssim_vals[index] = SSIM.CW_SSIM(
            i[0].ravel(), running_average[label].ravel())
        noisy_ssim_vals[index] = SSIM.SSIM(
            256 * i[0], 256 * running_average[label]) / 100.0

    print 'Mean of noisy cw ssim: {}'.format(np.mean(noisy_cw_ssim_vals))
    print 'Mean of noisy ssim: {}'.format(np.mean(noisy_ssim_vals))
    print 'std dev of noisy cw ssim: {}'.format(np.std(noisy_cw_ssim_vals))
    print 'std dev of noisy ssim: {}'.format(np.std(noisy_ssim_vals))
Exemple #3
0
    def write_parameters(self, data_path, binary=False, hex_=False, W=15, F=12):
        
        if binary and hex_:
            print "Cannot have both binary and hex be true."
            return

        if hex_:
            for count, layer in enumerate(self.biases):
                with open("{}/biases_{}.txt".format(data_path, count), 'w') as f:
                    for bias_count, bias in enumerate(layer):
                        f.write(SSIM.float_to_hex(bias) + "\n")
                    

            for layer_count, layer in enumerate(self.weights):
                for neuron_count, neuron in enumerate(layer):
                    with open("{}/weight_{}_{}.txt".format(data_path, layer_count, neuron_count), 'w') as f:
                        for weight_count, weight in enumerate(neuron):
                            f.write(SSIM.float_to_hex(weight) + "\n")
        elif not binary:
            for count, layer in enumerate(self.biases):
                np.savetxt("{}/biases_{}.txt".format(data_path, count), np.array(layer), delimiter = "\n")

            for layer_count, layer in enumerate(self.weights):
                for neuron_count, neuron in enumerate(layer):
                    np.savetxt("{}/weight_{}_{}.txt".format(data_path, layer_count, neuron_count), np.array(neuron), delimiter = "\n")
        else:
            for count, layer in enumerate(self.biases):
                with open("{}/biases_{}.txt".format(data_path, count), 'w') as f:
                    for bias_count, bias in enumerate(layer):
                        f.write(fixed_point.float2fix_bin(bias, W, F, twos_compliment=True) + "\n")
                    

            for layer_count, layer in enumerate(self.weights):
                for neuron_count, neuron in enumerate(layer):
                    with open("{}/weight_{}_{}.txt".format(data_path, layer_count, neuron_count), 'w') as f:
                        for weight_count, weight in enumerate(neuron):
                            f.write(fixed_point.float2fix_bin(weight, W, F, twos_compliment=True) + "\n")
Exemple #4
0
def detection(img):
    """
    Input: image

    Output: (x,y,weight,height) of region of interest

    """
    speed_limit = cv2.CascadeClassifier('USspeedlimit/cascade.xml')

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    height, weight = img.shape[:2]
    roi_gray = np.zeros((height, weight, 1), np.uint8)
    speed = speed_limit.detectMultiScale(gray)
    region = []
    for (x, y, w, h) in speed:
        region = [x, y, w, h]
        img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = img[y:y + h, x:x + w]

    if cv2.countNonZero(roi_gray) == 0:
        print "None region of interest detected."

    else:
        models = load_images('speedmodels')
        Smax = -2
        hroi, wroi = roi_gray.shape[:2]
        # recognition part
        for m in models:
            m = cv2.resize(m, (wroi, hroi))
            _, meanS = SSIM.structual_similarity_ssim(roi_gray, m)
            print meanS
            if meanS > Smax:
                Smax = meanS
                sim = m
        print "Similarity is %.2f" % Smax
        cv2.imshow('roi_gray', roi_gray)
        cv2.imshow('sim', sim)
        cv2.imshow('img', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    return region
Exemple #5
0
def master_metric(real_image, filled_image, a, b, c, type_):
    
    '''
        Takes as inputs :
            
            - real_image : The real image, format --> torch whose shape is (1,Spectrum_Size,m,n),
            - filled_image : The image filled by the estimator, format --> torch whose shape is (1,Spectrum_Size,m,n),
            
            - a,b,c : 3 parameters a,b,c >= 0 such as if type_== 'sum', master_metric = a x PSNR + b x SSIM + c x SAD,
                                                      if type_== 'product', master_metric = PSNR^a * SSIM^b * SAD^c,
            - type_ : 'sum' (if so, a+b+c=1) or 'product'.
            
        Returns :
        
            Return a metric which is a combinaison of the PSNR, the SSIM and the SAD metric (if a=1 and b,c=(0,0), the metric
            is equivalent to PSNR, if b=1 and a,c=(0,0), the metric is equivalent to SSIM and so on).
     
    '''
    
    try:
        a, b, c = np.abs(a), np.abs(b), np.abs(c)
    except:
        print("The parameters a,b,c must be numbers.")
    if a+b+c==0:
        raise ValueError("At least one parameter a,b or c should differ from 0.")
        
    ssim = SSIM.ssim(filled_image, real_image)
    loss_psnr = nn.MSELoss()
    psnr = 1/8*torch.log10(255*255/loss_psnr(filled_image, real_image))
    ri_f = torch.flatten(real_image)
    fi_f = torch.flatten(filled_image)
    sad = torch.dot(ri_f, fi_f)/(torch.norm(ri_f)*torch.norm(fi_f))
    
    if type_=='sum':
        a, b, c = a/(a+b+c), b/(a+b+c), c/(a+b+c)
        return a*psnr+b*ssim+c*sad
         
    elif type_=='product':
        return psnr**a * ssim**b * sad**c
    
    else:
        raise ValueError("\"type_\" must be 'sum' or 'product'.")
def Get_compare_video(video_input, bbox_input):
    filenames = glob.glob("labels/*.png")
    filenames = sorted(filenames, key=take_num_from_file)
    labels = [cv2.imread(img) for img in filenames]
    confidence_table = torch.zeros((video_input.shape[1], 82))
    confidence_table_ratio = torch.zeros((video_input.shape[1], 82))
    for i in range(video_input.shape[1]):  # video length
        bbox = bbox_input[:, i, :, :].permute(1, 2, 0)
        x, x_w, y, y_h = find_box_cords(bbox[:, :, 0])
        crop_frame = video_input[:, i, x:x_w, y:y_h]
        imgs_to_compare = torch.zeros((82, 3, x_w - x, y_h - y))
        imgs_real = torch.zeros((82, 3, x_w - x, y_h - y))
        for j, label in enumerate(labels):
            frame_ratio = crop_frame.shape[1] / crop_frame.shape[2]
            label_ratio = label.shape[0] / label.shape[1]
            confidence_table_ratio[i, j] = 1 if abs(frame_ratio -
                                                    label_ratio) < 0.5 else -1
            interpolation = cv2.INTER_LANCZOS4 if x_w - x > label.shape[
                1] else cv2.INTER_NEAREST
            label = cv2.resize(label, (y_h - y, x_w - x),
                               interpolation=interpolation)
            label = torch.tensor(cv2.cvtColor(label,
                                              cv2.COLOR_BGR2RGB)).permute(
                                                  2, 0, 1)
            imgs_to_compare[j] = label / 255
            imgs_real[j] = crop_frame
            # plt.imshow(label.permute(1, 2, 0) / 255)
            # plt.title(f'label is {str(j)}')
            # plt.show()
            # plt.imshow(crop_frame.permute(1, 2, 0))
            # plt.text = 'real'
            # plt.show()
        confidence_table[i] = SSIM.ssim(imgs_real,
                                        imgs_to_compare,
                                        size_average=False)
    return torch.minimum(confidence_table, confidence_table_ratio)
Exemple #7
0
def main():

    print('Loading dataset ...\n')
    dataset_train = Dataset(data_path=opt.data_path)
    loader_train = DataLoader(dataset=dataset_train,
                              num_workers=4,
                              batch_size=opt.batch_size,
                              shuffle=True)
    print("# of training samples: %d\n" % int(len(loader_train)))

    # Build model
    model = Network(nin=64, use_GPU=opt.use_GPU)
    print_network(model)

    # loss function
    criterion = SSIM()
    criterion1 = nn.L1Loss()
    criterion2 = nn.MSELoss()
    # Move to GPU
    if opt.use_GPU:
        model = model.cuda()
        criterion.cuda()
        criterion1.cuda()
        criterion2.cuda()

    # Optimizer
    optimizer = optim.Adam(model.parameters(), lr=opt.lr)
    scheduler = MultiStepLR(optimizer, milestones=opt.milestone,
                            gamma=0.2)  # learning rates,

    # record training
    writer = SummaryWriter(opt.save_path)

    # load the lastest model
    initial_epoch = findLastCheckpoint(save_dir=opt.save_path)
    if initial_epoch > 0:
        print('resuming by loading epoch %d' % initial_epoch)
        model.load_state_dict(
            torch.load(
                os.path.join(opt.save_path,
                             'net_epoch%d.pth' % initial_epoch)))

    # start training
    step = 0
    for epoch in range(initial_epoch, opt.epochs):
        scheduler.step(epoch)
        for param_group in optimizer.param_groups:
            print('learning rate %f' % param_group["lr"])

        ## epoch training start
        for i, (input_train, target_train) in enumerate(loader_train, 0):
            model.train()
            model.zero_grad()
            optimizer.zero_grad()

            input_train, target_train = Variable(input_train), Variable(
                target_train)

            if opt.use_GPU:
                input_train, target_train = input_train.cuda(
                ), target_train.cuda()

            out_train, r1, r2 = model(input_train)
            pixel_metric = criterion(target_train, out_train)
            loss1 = criterion(target_train, r1)
            loss2 = criterion(target_train, r2)
            loss3 = criterion1(target_train, out_train)
            #loss4 = criterion1(target_train, r1)
            #loss5=criterion1(target_train,r2)
            loss = -pixel_metric - loss1 - loss2 + loss3  #+loss4+loss5

            loss.backward()
            optimizer.step()

            # training curve
            model.eval()
            out_train, _, _ = model(input_train)
            out_train = torch.clamp(out_train, 0., 1.)
            psnr_train = batch_PSNR(out_train, target_train, 1.)
            print(
                "[epoch %d][%d/%d] loss: %.4f, pixel_metric: %.4f,loss1: %.4f,loss2: %.4f,loss3: %.4f,PSNR: %.4f"
                % (epoch + 1, i + 1, len(loader_train), loss.item(),
                   pixel_metric.item(), loss1.item(), loss2.item(),
                   loss3.item(), psnr_train))

            if step % 10 == 0:
                # Log the scalar values
                writer.add_scalar('loss', loss.item(), step)
                writer.add_scalar('PSNR on training data', psnr_train, step)
            step += 1
        ## epoch training end

        # log the images
        model.eval()
        out_train, _, _ = model(input_train)
        out_train = torch.clamp(out_train, 0., 1.)
        im_target = utils.make_grid(target_train.data,
                                    nrow=8,
                                    normalize=True,
                                    scale_each=True)
        im_input = utils.make_grid(input_train.data,
                                   nrow=8,
                                   normalize=True,
                                   scale_each=True)
        im_derain = utils.make_grid(out_train.data,
                                    nrow=8,
                                    normalize=True,
                                    scale_each=True)
        writer.add_image('clean image', im_target, epoch + 1)
        writer.add_image('rainy image', im_input, epoch + 1)
        writer.add_image('deraining image', im_derain, epoch + 1)

        # save model
        torch.save(model.state_dict(),
                   os.path.join(opt.save_path, 'net_latest.pth'))

        if epoch % opt.save_freq == 0:
            torch.save(
                model.state_dict(),
                os.path.join(opt.save_path, 'net_epoch%d.pth' % (epoch + 1)))
Exemple #8
0
def train(ssim_weight,
          original_imgs_path_name,
          source_a_imgs_path,
          source_b_imgs_path_name,
          encoder_path,
          save_path,
          model_pre_path,
          debug=False,
          logging_period=100):
    if debug:
        from datetime import datetime
        start_time = datetime.now()

    # num_imgs = len(source_a_imgs_path)
    num_imgs = 10000
    source_a_imgs_path = source_a_imgs_path[:num_imgs]
    mod = num_imgs % BATCH_SIZE

    print('Train images number %d.\n' % num_imgs)
    print('Train images samples %s.\n' % str(num_imgs / BATCH_SIZE))

    if mod > 0:
        print('Train set has been trimmed %d samples...\n' % mod)
        source_a_imgs_path = source_a_imgs_path[:-mod]

    # get the traing image shape
    HEIGHT, WIDTH, CHANNELS = TRAINING_IMAGE_SHAPE
    INPUT_SHAPE = (BATCH_SIZE, HEIGHT, WIDTH, CHANNELS)

    HEIGHT_OR, WIDTH_OR, CHANNELS_OR = TRAINING_IMAGE_SHAPE_OR
    INPUT_SHAPE_OR = (BATCH_SIZE, HEIGHT_OR, WIDTH_OR, CHANNELS_OR)

    # create the graph
    with tf.Graph().as_default(), tf.Session() as sess:
        original = tf.placeholder(tf.float32,
                                  shape=INPUT_SHAPE_OR,
                                  name='original')
        source_a = tf.placeholder(tf.float32,
                                  shape=INPUT_SHAPE,
                                  name='source_a')
        source_b = tf.placeholder(tf.float32,
                                  shape=INPUT_SHAPE,
                                  name='source_b')

        print('source:', source_a.shape)

        # create the style transfer net
        stn = StyleTransferNet(encoder_path, model_pre_path)

        # pass content and style to the stn, getting the generated_img, fused image
        generated_img = stn.transform(source_a, source_b)

        # # get the target feature maps which is the output of AdaIN
        # target_features = stn.target_features

        pixel_loss = tf.reduce_sum(
            tf.reduce_mean(tf.square(original - generated_img), axis=[1, 2]))
        pixel_loss = pixel_loss / (HEIGHT * WIDTH)

        # compute the SSIM loss
        ssim_loss = 1 - SSIM.tf_ssim(original, generated_img)

        # compute the total loss
        loss = pixel_loss + ssim_weight * ssim_loss

        # Training step
        train_op = tf.train.AdamOptimizer(LEARNING_RATE).minimize(loss)

        sess.run(tf.global_variables_initializer())

        # saver = tf.train.Saver()
        saver = tf.train.Saver(keep_checkpoint_every_n_hours=1)

        # ** Start Training **
        step = 0
        count_loss = 0
        n_batches = int(len(source_a_imgs_path) // BATCH_SIZE)

        if debug:
            elapsed_time = datetime.now() - start_time
            print(
                '\nElapsed time for preprocessing before actually train the model: %s'
                % elapsed_time)
            print('Now begin to train the model...\n')
            start_time = datetime.now()

        Loss_all = [i for i in range(EPOCHS * n_batches)]
        for epoch in range(EPOCHS):

            np.random.shuffle(source_a_imgs_path)

            for batch in range(n_batches):
                # retrive a batch of content and style images

                source_a_path = source_a_imgs_path[batch * BATCH_SIZE:(
                    batch * BATCH_SIZE + BATCH_SIZE)]
                source_a_str = source_a_path[0]
                name_f = source_a_str.find('\\')
                source_image_name = source_a_str[name_f + 1:]
                source_image_name_comm = source_image_name[2:]

                source_b_path = [source_b_imgs_path_name + source_image_name]
                original_path = [
                    original_imgs_path_name + source_image_name_comm
                ]

                original_batch = get_train_images(original_path,
                                                  crop_height=HEIGHT,
                                                  crop_width=WIDTH,
                                                  flag=False)
                source_a_batch = get_train_images(source_a_path,
                                                  crop_height=HEIGHT,
                                                  crop_width=WIDTH)
                source_b_batch = get_train_images(source_b_path,
                                                  crop_height=HEIGHT,
                                                  crop_width=WIDTH)

                original_batch = original_batch.reshape([1, 256, 256, 1])

                # run the training step
                sess.run(train_op,
                         feed_dict={
                             original: original_batch,
                             source_a: source_a_batch,
                             source_b: source_b_batch
                         })
                step += 1
                # if step % 1000 == 0:
                #     saver.save(sess, save_path, global_step=step)
                if debug:
                    is_last_step = (epoch == EPOCHS - 1) and (batch
                                                              == n_batches - 1)

                    if is_last_step or step % logging_period == 0:
                        elapsed_time = datetime.now() - start_time
                        _pixel_loss, _ssim_loss, _loss = sess.run(
                            [pixel_loss, ssim_loss, loss],
                            feed_dict={
                                original: original_batch,
                                source_a: source_a_batch,
                                source_b: source_b_batch
                            })
                        Loss_all[count_loss] = _loss
                        count_loss += 1
                        print(
                            'step: %d,  total loss: %.3f,  elapsed time: %s' %
                            (step, _loss, elapsed_time))
                        print('pixel loss: %.3f' % (_pixel_loss))
                        print('ssim loss : %.3f\n' % (_ssim_loss))
                        # print('pca or shape  : ', _pca_or.shape)
                        # print('pca gen shape : ', _pca_gen.shape)

        # ** Done Training & Save the model **
        saver.save(sess, save_path)

        iter_index = [i for i in range(count_loss)]
        plt.plot(iter_index, Loss_all[:count_loss])
        plt.show()

        if debug:
            elapsed_time = datetime.now() - start_time
            print('Done training! Elapsed time: %s' % elapsed_time)
            print('Model is saved to: %s' % save_path)
Exemple #9
0
def main():
    # Load dataset
    print('Loading dataset ...\n')
    dataset_train = Dataset(train=True, data_path=opt.data_path)
    loader_train = DataLoader(dataset=dataset_train,
                              num_workers=4,
                              batch_size=opt.batchSize,
                              shuffle=True)
    print("# of training samples: %d\n" % int(len(dataset_train)))
    # Build model

    model = DRN(channel=3,
                inter_iter=opt.inter_iter,
                intra_iter=opt.intra_iter,
                use_GPU=opt.use_GPU)
    print_network(model)

    criterion = SSIM()

    # Move to GPU
    if opt.use_GPU:
        model = model.cuda()
        criterion.cuda()
    # Optimizer
    optimizer = optim.Adam(model.parameters(), lr=opt.lr)
    scheduler = MultiStepLR(optimizer, milestones=opt.milestone,
                            gamma=0.5)  # learning rates
    # training
    writer = SummaryWriter(opt.save_folder)
    step = 0

    initial_epoch = findLastCheckpoint(
        save_dir=opt.save_folder)  # load the last model in matconvnet style
    if initial_epoch > 0:
        print('resuming by loading epoch %03d' % initial_epoch)
        model.load_state_dict(
            torch.load(
                os.path.join(opt.save_folder,
                             'net_epoch%d.pth' % initial_epoch)))

    for epoch in range(initial_epoch, opt.epochs):

        scheduler.step(epoch)
        # set learning rate
        for param_group in optimizer.param_groups:
            print('learning rate %f' % param_group["lr"])
        # train
        for i, (input, target) in enumerate(loader_train, 0):
            # training step
            loss_list = []
            model.train()
            model.zero_grad()
            optimizer.zero_grad()

            input_train, target_train = Variable(input.cuda()), Variable(
                target.cuda())

            out_train, outs = model(input_train)

            pixel_loss = criterion(target_train, out_train)

            for lossi in range(opt.inter_iter):
                loss1 = criterion(target_train, outs[lossi])
                loss_list.append(loss1)

            loss = -pixel_loss
            index = 0.1
            for lossi in range(opt.inter_iter):
                loss += -index * loss_list[lossi]
                index = index + 0.1
            loss.backward()
            optimizer.step()
            # results
            model.eval()
            out_train, _ = model(input_train)
            out_train = torch.clamp(out_train, 0., 1.)
            psnr_train = batch_PSNR(out_train, target_train, 1.)
            print(
                "[epoch %d][%d/%d] loss: %.4f, loss1: %.4f, loss2: %.4f, loss3: %.4f, loss4: %.4f, PSNR_train: %.4f"
                % (epoch + 1, i + 1, len(loader_train), loss.item(),
                   loss_list[0].item(), loss_list[1].item(),
                   loss_list[2].item(), loss_list[3].item(), psnr_train))
            # print("[epoch %d][%d/%d] loss: %.4f, PSNR_train: %.4f" %
            #       (epoch + 1, i + 1, len(loader_train), loss.item(), psnr_train))
            # if you are using older version of PyTorch, you may need to change loss.item() to loss.data[0]
            if step % 10 == 0:
                # Log the scalar values
                writer.add_scalar('loss', loss.item(), step)
                writer.add_scalar('PSNR on training data', psnr_train, step)
            step += 1
        ## the end of each epoch

        model.eval()

        # log the images
        out_train, _ = model(input_train)
        out_train = torch.clamp(out_train, 0., 1.)
        Img = utils.make_grid(target_train.data,
                              nrow=8,
                              normalize=True,
                              scale_each=True)
        Imgn = utils.make_grid(input_train.data,
                               nrow=8,
                               normalize=True,
                               scale_each=True)
        Irecon = utils.make_grid(out_train.data,
                                 nrow=8,
                                 normalize=True,
                                 scale_each=True)
        writer.add_image('clean image', Img, epoch)
        writer.add_image('noisy image', Imgn, epoch)
        writer.add_image('reconstructed image', Irecon, epoch)
        # save model
        torch.save(model.state_dict(),
                   os.path.join(opt.save_folder, 'net_latest.pth'))

        if epoch % opt.save_freq == 0:
            torch.save(
                model.state_dict(),
                os.path.join(opt.save_folder, 'net_epoch%d.pth' % (epoch + 1)))
Exemple #10
0
def run_framework(training_threshold, confidence_threshold, ssim_threshold,
                  num_train):
    '''
	These are statistics to see how useful the SSIM and confidence check are, over time
	'''
    ssim_true_positive = 0
    confidence_true_positive = 0
    confidence_false_positive = 0
    ssim_false_positive = 0

    num_retrain = 0
    num_missed = 0
    '''
	Initializing framework necessary things
	'''
    running_average = [np.zeros((784, 1), dtype='float32')] * 10
    training_images = []
    num_incorrect = 0
    '''
	Creating the network, clean data, and noisy data
	'''
    net = network.Network(
        [784, 30, 10])  # The network that we will adaptivally train with
    training_data, validation_data, testing_data = mnist_loader.load_data_wrapper(
    )
    noisy_testing_data, noisy_training_data = post_processing.create_two_noisy_data(
        testing_data, training_data, 80)
    '''
	Training the network on the clean data.
	'''
    net = network.Network([784, 30, 10])
    net.SGD(training_data, 10, 10, 3.0)

    original_clean_accuracy = net.evaluate(testing_data) / 10000.0
    original_noisy_accuracy = net.evaluate(noisy_testing_data) / 10000.0
    '''
	Creating the running average array from training_data. We then 
	create the image of all average to make sure that it looks good.
	'''
    count = [0] * 10
    for i in training_data:
        label = np.argmax(i[1])
        count[label] = count[label] + 1
        running_average[label] = running_average[label] + i[0]

    for index, i in enumerate(count):
        running_average[index] = running_average[index] / count[index]
    '''
	Actually going through the adaptive training for net. Similar to deploying into real world.
	'''
    for train_iter in range(num_train):
        '''
		Network sees image in the field. It produces 10 outputs
		'''
        test_index = random.randint(0, 10000)
        curr_image = noisy_training_data[test_index]
        network_output = net.feedforward(curr_image[0])
        network_prediction = np.argmax(network_output)
        network_confidence = np.max(network_output)

        if network_confidence < confidence_threshold:
            training_images.append(curr_image)
            num_incorrect = num_incorrect + 1
            confidence_true_positive = confidence_true_positive + int(
                not network_prediction == np.argmax(curr_image[1]))
            confidence_false_positive = confidence_false_positive + int(
                network_prediction == np.argmax(curr_image[1]))
        else:
            #ssim_val = SSIM.CW_SSIM(curr_image[0].ravel(), running_average[network_prediction].ravel())
            ssim_val = SSIM.SSIM(256 * curr_image[0], 256 *
                                 running_average[network_prediction]) / 100.0

            if ssim_val < ssim_threshold:
                training_images.append(curr_image)
                num_incorrect = num_incorrect + 1
                ssim_true_positive = ssim_true_positive + int(
                    not network_prediction == np.argmax(curr_image[1]))
                ssim_false_positive = ssim_false_positive + int(
                    network_prediction == np.argmax(curr_image[1]))
            else:
                num_missed = num_missed + int(
                    not network_prediction == np.argmax(curr_image[1])
                )  # Will go into here if it is fine. Adding if it is wrong.

        if num_incorrect >= training_threshold:
            num_retrain = num_retrain + 1
            net.SGD(training_images, 10, 10, 3.0)
            training_images = []
            num_incorrect = 0
            for i in training_data:
                label = np.argmax(i[1])
                count[label] = count[label] + 1
                running_average[label] = running_average[label] + i[0]

            for index, i in enumerate(count):
                running_average[index] = running_average[index] / count[index]
    '''
	Seeing how accurate we are on both the noisy and not-noisy testing data
	'''

    clean_diff = original_clean_accuracy - net.evaluate(testing_data) / 10000.0
    noisy_diff = net.evaluate(
        noisy_testing_data) / 10000.0 - original_noisy_accuracy
    ssim_triggers = ssim_false_positive + ssim_true_positive
    confidence_triggers = confidence_false_positive + confidence_true_positive
    try:
        ssim_true_positive_rate = ssim_true_positive * 1.0 / ssim_triggers
    except:
        ssim_true_positive_rate = 0

    try:
        confidence_true_positive_rate = confidence_true_positive * 1.0 / confidence_triggers
    except:
        confidence_true_positive_rate = 0

    print "{}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}".format(
        ssim_threshold, confidence_threshold, training_threshold, clean_diff,
        noisy_diff, num_retrain, num_missed, ssim_triggers,
        confidence_triggers, ssim_true_positive_rate,
        confidence_true_positive_rate)