cnn.add_convolutional_layer(5, 36)
cnn.add_flat_layer()
cnn.add_fc_layer(size=64, use_relu=True)
cnn.add_fc_layer(size=16, use_relu=True)
cnn.add_fc_layer(size=2, use_relu=False)
cnn.finish_setup()

model_path = os.path.join(cwd, 'results', 'nn_models',
                          'crater_east_model_nn.ckpt')
model.restore(model_path)

cnn_model_path = os.path.join(cwd, 'results/models/crater_west_model_cnn.ckpt')
cnn.restore(cnn_model_path)

image1 = data.test.images[7]
p_nn_non, nn_p = model.predict([image1])[0]

p_non, p_crater = cnn.predict([image1])[0]

print("nn predict: " + str(nn_p))

print("cnn predict: " + str(cnn_p))

#model.print_test_accuracy()

#model.optimize(epochs=20)

#model.save(model_path)

#model.print_test_accuracy()
        print("Resized shape: %d, Window size: %d, i: %d" %
              (resized.shape[0], winS, i))

        # loop over the sliding window for each layer of the pyramid
        # this process takes about 7 hours. To do quick test, we may try stepSize
        # to be large (60) and see if code runs OK
        for (x, y, window) in sliding_window(resized,
                                             stepSize=8,
                                             windowSize=(winS, winS)):

            # apply a circular mask ot the window here. Think about where you should apply this mask. Before, resizing or after it.
            crop_img = cv.resize(window, (50, 50))
            cv.normalize(crop_img, crop_img, 0, 255, cv.NORM_MINMAX)
            crop_img = crop_img.flatten()

            p_non, p_crater = cnn.predict([crop_img])[0]

            scale_factor = 1.5**i
            x_c = int((x + 0.5 * winS) * scale_factor)
            y_c = int((y + 0.5 * winS) * scale_factor)
            crater_r = int(winS * scale_factor / 2)

            # add its probability to a score combined thresholded image and normal iamges.

            if p_crater >= 0.75:
                crater_data = [x_c, y_c, crater_r, p_crater]
                crater_list_cnn.append(crater_data)

    cnn_file = open("results/cnn/" + tile_img + "_sw_cnn_th.csv", "w")
    with cnn_file:
        writer = csv.writer(cnn_file, delimiter=',')
for (i, resized) in enumerate(pyramid_gaussian(img, downscale=1.5)):
    if resized.shape[0] < 30:
        break
    for winS in win_sizes:
        # loop over the sliding window for each layer of the pyramid
        for (x, y, window) in sliding_window(resized,
                                             stepSize=60,
                                             windowSize=(winS, winS)):
            # since we do not have a classifier, we'll just draw the window
            clone = resized.copy()
            y_b = y + winS
            x_r = x + winS
            crop_img = clone[y:y_b, x:x_r]
            crop_img = cv.resize(crop_img, (30, 30))
            crop_img = crop_img.flatten()
            p_non, p_crater = model.predict([crop_img])[0]
            scale_factor = 1.5**i
            if p_crater >= 0.5:
                x_c = int((x + 0.5 * winS) * scale_factor)
                y_c = int((y + 0.5 * winS) * scale_factor)
                crater_size = int(winS * scale_factor)
                crater_data = [x_c, y_c, crater_size, p_crater, 1]
                crater_list.append(crater_data)
            # if we want to see where is processed.
            # cv.rectangle(clone, (x, y), (x + winS, y + winS), (0, 255, 0), 2)
            # cv.imshow("Window", clone)
            # cv.waitKey(1)
out = csv.writer(open("crater_24.csv", "w"),
                 delimiter=',',
                 quoting=csv.QUOTE_ALL)
out.writerow(crater_list)
Пример #4
0
        # loop over the sliding window for each layer of the pyramid
        # this process takes about 7 hours. To do quick test, we may try stepSize
        # to be large (60) and see if code runs OK
        #for (x, y, window) in sliding_window(resized, stepSize=2, windowSize=(winS, winS)):
        for (x, y, window) in sliding_window(resized,
                                             stepSize=60,
                                             windowSize=(winS, winS)):
            # since we do not have a classifier, we'll just draw the window
            clone = resized.copy()
            y_b = y + winS
            x_r = x + winS
            crop_img = clone[y:y_b, x:x_r]
            crop_img = cv.resize(crop_img, (50, 50))
            crop_img = crop_img.flatten()

            p_non, p_crater = cnn.predict([crop_img])[0]
            p_nn_non, nn_p = nn.predict([crop_img])[0]
            #nn_p = nn.feedforward_flat(crop_img)[0,0]

            scale_factor = 1.5**i
            if p_crater >= 0.5 or nn_p >= 0.5:
                x_c = int((x + 0.5 * winS) * scale_factor)
                y_c = int((y + 0.5 * winS) * scale_factor)
                crater_size = int(winS * scale_factor)

                if p_crater >= 0.5:
                    crater_data = [x_c, y_c, crater_size, p_crater, 1]
                    crater_list_cnn.append(crater_data)
                if nn_p >= 0.5:
                    crater_data = [x_c, y_c, crater_size, nn_p, 1]
                    crater_list_nn.append(crater_data)
Пример #5
0
from crater_data import Data

# Load data
images, labels, hot_one = load_crater_data()
data = Data(images, hot_one, random_state=42)

model = Network(img_shape=(30, 30, 1))
model.add_convolutional_layer(5, 16)
model.add_convolutional_layer(5, 36)
model.add_flat_layer()
model.add_fc_layer(size=128, use_relu=True)
model.add_fc_layer(size=2, use_relu=False)
model.finish_setup()
model.set_data(data)

model_path = os.path.join(cwd, 'model.ckpt')
model.restore(model_path)

image1 = data.test.images[7]
image2 = data.test.images[14]

print(model.predict([image1]))
print(model.predict([image1, image2]))

samples = [image1, image2]
print(model.predict(samples))

result = data.test.cls[0], data.test.labels[0], model.predict(
    [data.test.images[0]])
print(result)