Loading .ipynb_checkpoints/prediction_MSE-checkpoint.ipynb +1 −1 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time ``` ``` %% Cell type:code id:b7a550e0 tags: %% Cell type:code id:b7a550e0 tags: ``` python ``` python def file_extractor(dirname="images"): def file_extractor(dirname="images"): files = os.listdir(dirname) files = os.listdir(dirname) scenes = [] scenes = [] for file in files: for file in files: scenes.append(os.path.join(dirname, file)) scenes.append(os.path.join(dirname, file)) return scenes return scenes def image_extractor(scenes): def image_extractor(scenes): image_folder = [] image_folder = [] for scene in scenes: for scene in scenes: files = os.listdir(scene) files = os.listdir(scene) for file in files: for file in files: image_folder.append(os.path.join(scene, file)) image_folder.append(os.path.join(scene, file)) images = [] images = [] for folder in image_folder: for folder in image_folder: ims = os.listdir(folder) ims = os.listdir(folder) for im in ims: for im in ims: if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": continue continue else: else: images.append(os.path.join(folder, im)) images.append(os.path.join(folder, im)) return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor def im_distribution(images, num): def im_distribution(images, num): """ """ Function that extracts tiff files from specific cameras and returns a list of all Function that extracts tiff files from specific cameras and returns a list of all the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise specified camera numbers. specified camera numbers. Parameters: Parameters: images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but of specific tiff files that can be opened right away. This is the list that we iterate through and of specific tiff files that can be opened right away. This is the list that we iterate through and divide. divide. num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits of "_1" for single digits. of "_1" for single digits. Returns: Returns: tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted from the 'images' list that correspond to the given num. from the 'images' list that correspond to the given num. """ """ tiff = [] tiff = [] for im in images: for im in images: if im[-7:-5] == num: if im[-7:-5] == num: tiff.append(im) tiff.append(im) return tiff return tiff ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i): def plot_hist(tiff_list, i): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = image.astype(int) image = image.astype(int) row, col = image.shape row, col = image.shape predict = np.empty([row,col]) # create a empty matrix to update prediction predict = np.empty([row,col]) # create a empty matrix to update prediction predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image diff = np.empty([row,col]) diff = np.empty([row,col]) diff[0,:] = np.zeros(col) # keep the first row from the image diff[0,:] = np.zeros(col) # keep the first row from the image diff[:,0] = np.zeros(row) diff[:,0] = np.zeros(row) diff[-1,:] = np.zeros(col) # keep the first row from the image diff[-1,:] = np.zeros(col) # keep the first row from the image diff[:,-1] = np.zeros(row) diff[:,-1] = np.zeros(row) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) '''z0 = image[0:-2,0:-2] '''z0 = image[0:-2,0:-2] z1 = image[0:-2,1:-1] z1 = image[0:-2,1:-1] z2 = image[0:-2,2::] z2 = image[0:-2,2::] z3 = image[1:-1,0:-2] z3 = image[1:-1,0:-2] y0 = -z0+z2-z3 y0 = -z0+z2-z3 y1 = z0+z1+z2 y1 = z0+z1+z2 y2 = -z0-z1-z2-z3 y2 = -z0-z1-z2-z3 predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] ''' ''' for r in range(1,row-1): # loop through the rth row for r in range(1,row-1): # loop through the rth row for c in range(1,col-1): # loop through the cth column for c in range(1,col-1): # loop through the cth column actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) predict[r,c] = np.linalg.solve(A,y)[-1] predict[r,c] = np.linalg.solve(A,y)[-1] diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) predict = np.ravel(predict[1:-1,1:-1]) predict = np.ravel(predict[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) return image, predict, diff return image, predict, diff ``` ``` %% Cell type:code id:8e3ef654 tags: %% Cell type:code id:8e3ef654 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") error_mean = [] error_mean = [] error_mean1 = [] error_mean1 = [] diff_mean = [] diff_mean = [] times = [] times = [] times1 = [] times1 = [] all_error = [] all_error = [] for i in range(len(num_images)): for i in range(len(num_images)): """start1 = time() """start1 = time() image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") stop1 = time() stop1 = time() times1.append(stop1-start1) times1.append(stop1-start1) error1 = np.abs(image_1-predict_1) error1 = np.abs(image_1-predict_1) error_mean1.append(np.mean(np.ravel(error1)))""" error_mean1.append(np.mean(np.ravel(error1)))""" start = time() start = time() image, predict, difference = plot_hist(num_images, i) image, predict, difference = plot_hist(num_images, i) stop = time() stop = time() times.append(stop-start) times.append(stop-start) error = np.abs(image-predict) error = np.abs(image-predict) all_error.append(np.ravel(error)) all_error.append(np.ravel(error)) error_mean.append(np.mean(np.ravel(error))) error_mean.append(np.mean(np.ravel(error))) diff_mean.append(np.mean(np.ravel(difference))) diff_mean.append(np.mean(np.ravel(difference))) ``` ``` %% Cell type:code id:a51dcb6f tags: %% Cell type:code id:fa65dcd6 tags: ``` python ``` python print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Time per Image for First: {np.mean(times)}") print(f"Average Time per Image for First: {np.mean(times)}") ``` ``` %% Output %% Output Average Error First and Second Added: 20.017164930235467 Average Error First and Second Added: 20.017164930235467 Standard Deviaiton of Mean Errors: 0.16101183692474846 Standard Deviaiton of Mean Errors: 0.16101183692474846 Average Difference: 53.678648426455226 Average Difference: 53.678648426455226 Average Time per Image for First: 9.85209345817566 Average Time per Image for First: 9.85209345817566 %% Cell type:code id:dda442ae tags: %% Cell type:code id:dda442ae tags: ``` python ``` python fig = plt.figure(figsize = (10,10)) fig = plt.figure(figsize = (10,10)) ax = fig.add_subplot() ax = fig.add_subplot() x = np.abs(predict-image) x = np.abs(predict-image) y = diff y = diff plt.plot(x,y,'o',alpha = 0.2) plt.plot(x,y,'o',alpha = 0.2) plt.rcParams.update({'font.size': 20}) plt.rcParams.update({'font.size': 20}) plt.xlabel("differnece to the true value" ) plt.xlabel("differnece to the true value" ) plt.ylabel("differnece of min and max of true value of the surroundings") plt.ylabel("differnece of min and max of true value of the surroundings") plt.show() plt.show() ``` ``` %% Output %% Output %% Cell type:code id:58da6063 tags: %% Cell type:code id:58da6063 tags: ``` python ``` python image = Image.open(images[0]) #Open the image and read it as an Image object image = Image.open(images[0]) #Open the image and read it as an Image object image = np.array(image)[1:,:] image = np.array(image)[1:,:] #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) z = np.array([22554,22552,22519,22561]) z = np.array([22554,22552,22519,22561]) print(z) print(z) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) a,b,c = np.linalg.solve(A,y)''' a,b,c = np.linalg.solve(A,y)''' A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(y) print(y) a,b,c = np.linalg.solve(A,y) a,b,c = np.linalg.solve(A,y) print(a,b,c) print(a,b,c) ``` ``` %% Output %% Output [22554 22552 22519 22561] [22554 22552 22519 22561] [-22596 67625 -90186] [-22596 67625 -90186] -17.49999999999879 -1.8333333333369712 22543.500000000004 -17.49999999999879 -1.8333333333369712 22543.500000000004 %% Cell type:code id:2562feeb tags: %% Cell type:code id:2562feeb tags: ``` python ``` python i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 160.16666666662906 160.16666666662906 %% Cell type:code id:470cc137 tags: %% Cell type:code id:470cc137 tags: ``` python ``` python a = 0 a = 0 b = 2 b = 2 c = 2 c = 2 i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 2032748510 2032748510 %% Cell type:code id:3292b395 tags: %% Cell type:code id:3292b395 tags: ``` python ``` python z = np.hstack((image[0,:3], image[1,0])) z = np.hstack((image[0,:3], image[1,0])) x = np.array([-1,0,1,-1]) x = np.array([-1,0,1,-1]) y = np.array([-1,-1,-1,0]) y = np.array([-1,-1,-1,0]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) ``` ``` %% Output %% Output -75749.00000000001 -75749.00000000001 C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) %% Cell type:code id:f9687830 tags: %% Cell type:code id:f9687830 tags: ``` python ``` python 0.5**2 + 1.5**2 0.5**2 + 1.5**2 ``` ``` %% Cell type:code id:e98eed4b tags: %% Cell type:code id:e98eed4b tags: ``` python ``` python ``` ``` prediction_MSE.ipynb +1 −1 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time ``` ``` %% Cell type:code id:b7a550e0 tags: %% Cell type:code id:b7a550e0 tags: ``` python ``` python def file_extractor(dirname="images"): def file_extractor(dirname="images"): files = os.listdir(dirname) files = os.listdir(dirname) scenes = [] scenes = [] for file in files: for file in files: scenes.append(os.path.join(dirname, file)) scenes.append(os.path.join(dirname, file)) return scenes return scenes def image_extractor(scenes): def image_extractor(scenes): image_folder = [] image_folder = [] for scene in scenes: for scene in scenes: files = os.listdir(scene) files = os.listdir(scene) for file in files: for file in files: image_folder.append(os.path.join(scene, file)) image_folder.append(os.path.join(scene, file)) images = [] images = [] for folder in image_folder: for folder in image_folder: ims = os.listdir(folder) ims = os.listdir(folder) for im in ims: for im in ims: if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": continue continue else: else: images.append(os.path.join(folder, im)) images.append(os.path.join(folder, im)) return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor def im_distribution(images, num): def im_distribution(images, num): """ """ Function that extracts tiff files from specific cameras and returns a list of all Function that extracts tiff files from specific cameras and returns a list of all the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise specified camera numbers. specified camera numbers. Parameters: Parameters: images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but of specific tiff files that can be opened right away. This is the list that we iterate through and of specific tiff files that can be opened right away. This is the list that we iterate through and divide. divide. num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits of "_1" for single digits. of "_1" for single digits. Returns: Returns: tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted from the 'images' list that correspond to the given num. from the 'images' list that correspond to the given num. """ """ tiff = [] tiff = [] for im in images: for im in images: if im[-7:-5] == num: if im[-7:-5] == num: tiff.append(im) tiff.append(im) return tiff return tiff ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i): def plot_hist(tiff_list, i): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = image.astype(int) image = image.astype(int) row, col = image.shape row, col = image.shape predict = np.empty([row,col]) # create a empty matrix to update prediction predict = np.empty([row,col]) # create a empty matrix to update prediction predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image diff = np.empty([row,col]) diff = np.empty([row,col]) diff[0,:] = np.zeros(col) # keep the first row from the image diff[0,:] = np.zeros(col) # keep the first row from the image diff[:,0] = np.zeros(row) diff[:,0] = np.zeros(row) diff[-1,:] = np.zeros(col) # keep the first row from the image diff[-1,:] = np.zeros(col) # keep the first row from the image diff[:,-1] = np.zeros(row) diff[:,-1] = np.zeros(row) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) '''z0 = image[0:-2,0:-2] '''z0 = image[0:-2,0:-2] z1 = image[0:-2,1:-1] z1 = image[0:-2,1:-1] z2 = image[0:-2,2::] z2 = image[0:-2,2::] z3 = image[1:-1,0:-2] z3 = image[1:-1,0:-2] y0 = -z0+z2-z3 y0 = -z0+z2-z3 y1 = z0+z1+z2 y1 = z0+z1+z2 y2 = -z0-z1-z2-z3 y2 = -z0-z1-z2-z3 predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] ''' ''' for r in range(1,row-1): # loop through the rth row for r in range(1,row-1): # loop through the rth row for c in range(1,col-1): # loop through the cth column for c in range(1,col-1): # loop through the cth column actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) predict[r,c] = np.linalg.solve(A,y)[-1] predict[r,c] = np.linalg.solve(A,y)[-1] diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) predict = np.ravel(predict[1:-1,1:-1]) predict = np.ravel(predict[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) return image, predict, diff return image, predict, diff ``` ``` %% Cell type:code id:8e3ef654 tags: %% Cell type:code id:8e3ef654 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") error_mean = [] error_mean = [] error_mean1 = [] error_mean1 = [] diff_mean = [] diff_mean = [] times = [] times = [] times1 = [] times1 = [] all_error = [] all_error = [] for i in range(len(num_images)): for i in range(len(num_images)): """start1 = time() """start1 = time() image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") stop1 = time() stop1 = time() times1.append(stop1-start1) times1.append(stop1-start1) error1 = np.abs(image_1-predict_1) error1 = np.abs(image_1-predict_1) error_mean1.append(np.mean(np.ravel(error1)))""" error_mean1.append(np.mean(np.ravel(error1)))""" start = time() start = time() image, predict, difference = plot_hist(num_images, i) image, predict, difference = plot_hist(num_images, i) stop = time() stop = time() times.append(stop-start) times.append(stop-start) error = np.abs(image-predict) error = np.abs(image-predict) all_error.append(np.ravel(error)) all_error.append(np.ravel(error)) error_mean.append(np.mean(np.ravel(error))) error_mean.append(np.mean(np.ravel(error))) diff_mean.append(np.mean(np.ravel(difference))) diff_mean.append(np.mean(np.ravel(difference))) ``` ``` %% Cell type:code id:a51dcb6f tags: %% Cell type:code id:fa65dcd6 tags: ``` python ``` python print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Time per Image for First: {np.mean(times)}") print(f"Average Time per Image for First: {np.mean(times)}") ``` ``` %% Output %% Output Average Error First and Second Added: 20.017164930235467 Average Error First and Second Added: 20.017164930235467 Standard Deviaiton of Mean Errors: 0.16101183692474846 Standard Deviaiton of Mean Errors: 0.16101183692474846 Average Difference: 53.678648426455226 Average Difference: 53.678648426455226 Average Time per Image for First: 9.85209345817566 Average Time per Image for First: 9.85209345817566 %% Cell type:code id:dda442ae tags: %% Cell type:code id:dda442ae tags: ``` python ``` python fig = plt.figure(figsize = (10,10)) fig = plt.figure(figsize = (10,10)) ax = fig.add_subplot() ax = fig.add_subplot() x = np.abs(predict-image) x = np.abs(predict-image) y = diff y = diff plt.plot(x,y,'o',alpha = 0.2) plt.plot(x,y,'o',alpha = 0.2) plt.rcParams.update({'font.size': 20}) plt.rcParams.update({'font.size': 20}) plt.xlabel("differnece to the true value" ) plt.xlabel("differnece to the true value" ) plt.ylabel("differnece of min and max of true value of the surroundings") plt.ylabel("differnece of min and max of true value of the surroundings") plt.show() plt.show() ``` ``` %% Output %% Output %% Cell type:code id:58da6063 tags: %% Cell type:code id:58da6063 tags: ``` python ``` python image = Image.open(images[0]) #Open the image and read it as an Image object image = Image.open(images[0]) #Open the image and read it as an Image object image = np.array(image)[1:,:] image = np.array(image)[1:,:] #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) z = np.array([22554,22552,22519,22561]) z = np.array([22554,22552,22519,22561]) print(z) print(z) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) a,b,c = np.linalg.solve(A,y)''' a,b,c = np.linalg.solve(A,y)''' A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(y) print(y) a,b,c = np.linalg.solve(A,y) a,b,c = np.linalg.solve(A,y) print(a,b,c) print(a,b,c) ``` ``` %% Output %% Output [22554 22552 22519 22561] [22554 22552 22519 22561] [-22596 67625 -90186] [-22596 67625 -90186] -17.49999999999879 -1.8333333333369712 22543.500000000004 -17.49999999999879 -1.8333333333369712 22543.500000000004 %% Cell type:code id:2562feeb tags: %% Cell type:code id:2562feeb tags: ``` python ``` python i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 160.16666666662906 160.16666666662906 %% Cell type:code id:470cc137 tags: %% Cell type:code id:470cc137 tags: ``` python ``` python a = 0 a = 0 b = 2 b = 2 c = 2 c = 2 i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 2032748510 2032748510 %% Cell type:code id:3292b395 tags: %% Cell type:code id:3292b395 tags: ``` python ``` python z = np.hstack((image[0,:3], image[1,0])) z = np.hstack((image[0,:3], image[1,0])) x = np.array([-1,0,1,-1]) x = np.array([-1,0,1,-1]) y = np.array([-1,-1,-1,0]) y = np.array([-1,-1,-1,0]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) ``` ``` %% Output %% Output -75749.00000000001 -75749.00000000001 C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) %% Cell type:code id:f9687830 tags: %% Cell type:code id:f9687830 tags: ``` python ``` python 0.5**2 + 1.5**2 0.5**2 + 1.5**2 ``` ``` %% Cell type:code id:e98eed4b tags: %% Cell type:code id:e98eed4b tags: ``` python ``` python ``` ``` Loading
.ipynb_checkpoints/prediction_MSE-checkpoint.ipynb +1 −1 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time ``` ``` %% Cell type:code id:b7a550e0 tags: %% Cell type:code id:b7a550e0 tags: ``` python ``` python def file_extractor(dirname="images"): def file_extractor(dirname="images"): files = os.listdir(dirname) files = os.listdir(dirname) scenes = [] scenes = [] for file in files: for file in files: scenes.append(os.path.join(dirname, file)) scenes.append(os.path.join(dirname, file)) return scenes return scenes def image_extractor(scenes): def image_extractor(scenes): image_folder = [] image_folder = [] for scene in scenes: for scene in scenes: files = os.listdir(scene) files = os.listdir(scene) for file in files: for file in files: image_folder.append(os.path.join(scene, file)) image_folder.append(os.path.join(scene, file)) images = [] images = [] for folder in image_folder: for folder in image_folder: ims = os.listdir(folder) ims = os.listdir(folder) for im in ims: for im in ims: if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": continue continue else: else: images.append(os.path.join(folder, im)) images.append(os.path.join(folder, im)) return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor def im_distribution(images, num): def im_distribution(images, num): """ """ Function that extracts tiff files from specific cameras and returns a list of all Function that extracts tiff files from specific cameras and returns a list of all the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise specified camera numbers. specified camera numbers. Parameters: Parameters: images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but of specific tiff files that can be opened right away. This is the list that we iterate through and of specific tiff files that can be opened right away. This is the list that we iterate through and divide. divide. num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits of "_1" for single digits. of "_1" for single digits. Returns: Returns: tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted from the 'images' list that correspond to the given num. from the 'images' list that correspond to the given num. """ """ tiff = [] tiff = [] for im in images: for im in images: if im[-7:-5] == num: if im[-7:-5] == num: tiff.append(im) tiff.append(im) return tiff return tiff ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i): def plot_hist(tiff_list, i): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = image.astype(int) image = image.astype(int) row, col = image.shape row, col = image.shape predict = np.empty([row,col]) # create a empty matrix to update prediction predict = np.empty([row,col]) # create a empty matrix to update prediction predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image diff = np.empty([row,col]) diff = np.empty([row,col]) diff[0,:] = np.zeros(col) # keep the first row from the image diff[0,:] = np.zeros(col) # keep the first row from the image diff[:,0] = np.zeros(row) diff[:,0] = np.zeros(row) diff[-1,:] = np.zeros(col) # keep the first row from the image diff[-1,:] = np.zeros(col) # keep the first row from the image diff[:,-1] = np.zeros(row) diff[:,-1] = np.zeros(row) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) '''z0 = image[0:-2,0:-2] '''z0 = image[0:-2,0:-2] z1 = image[0:-2,1:-1] z1 = image[0:-2,1:-1] z2 = image[0:-2,2::] z2 = image[0:-2,2::] z3 = image[1:-1,0:-2] z3 = image[1:-1,0:-2] y0 = -z0+z2-z3 y0 = -z0+z2-z3 y1 = z0+z1+z2 y1 = z0+z1+z2 y2 = -z0-z1-z2-z3 y2 = -z0-z1-z2-z3 predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] ''' ''' for r in range(1,row-1): # loop through the rth row for r in range(1,row-1): # loop through the rth row for c in range(1,col-1): # loop through the cth column for c in range(1,col-1): # loop through the cth column actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) predict[r,c] = np.linalg.solve(A,y)[-1] predict[r,c] = np.linalg.solve(A,y)[-1] diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) predict = np.ravel(predict[1:-1,1:-1]) predict = np.ravel(predict[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) return image, predict, diff return image, predict, diff ``` ``` %% Cell type:code id:8e3ef654 tags: %% Cell type:code id:8e3ef654 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") error_mean = [] error_mean = [] error_mean1 = [] error_mean1 = [] diff_mean = [] diff_mean = [] times = [] times = [] times1 = [] times1 = [] all_error = [] all_error = [] for i in range(len(num_images)): for i in range(len(num_images)): """start1 = time() """start1 = time() image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") stop1 = time() stop1 = time() times1.append(stop1-start1) times1.append(stop1-start1) error1 = np.abs(image_1-predict_1) error1 = np.abs(image_1-predict_1) error_mean1.append(np.mean(np.ravel(error1)))""" error_mean1.append(np.mean(np.ravel(error1)))""" start = time() start = time() image, predict, difference = plot_hist(num_images, i) image, predict, difference = plot_hist(num_images, i) stop = time() stop = time() times.append(stop-start) times.append(stop-start) error = np.abs(image-predict) error = np.abs(image-predict) all_error.append(np.ravel(error)) all_error.append(np.ravel(error)) error_mean.append(np.mean(np.ravel(error))) error_mean.append(np.mean(np.ravel(error))) diff_mean.append(np.mean(np.ravel(difference))) diff_mean.append(np.mean(np.ravel(difference))) ``` ``` %% Cell type:code id:a51dcb6f tags: %% Cell type:code id:fa65dcd6 tags: ``` python ``` python print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Time per Image for First: {np.mean(times)}") print(f"Average Time per Image for First: {np.mean(times)}") ``` ``` %% Output %% Output Average Error First and Second Added: 20.017164930235467 Average Error First and Second Added: 20.017164930235467 Standard Deviaiton of Mean Errors: 0.16101183692474846 Standard Deviaiton of Mean Errors: 0.16101183692474846 Average Difference: 53.678648426455226 Average Difference: 53.678648426455226 Average Time per Image for First: 9.85209345817566 Average Time per Image for First: 9.85209345817566 %% Cell type:code id:dda442ae tags: %% Cell type:code id:dda442ae tags: ``` python ``` python fig = plt.figure(figsize = (10,10)) fig = plt.figure(figsize = (10,10)) ax = fig.add_subplot() ax = fig.add_subplot() x = np.abs(predict-image) x = np.abs(predict-image) y = diff y = diff plt.plot(x,y,'o',alpha = 0.2) plt.plot(x,y,'o',alpha = 0.2) plt.rcParams.update({'font.size': 20}) plt.rcParams.update({'font.size': 20}) plt.xlabel("differnece to the true value" ) plt.xlabel("differnece to the true value" ) plt.ylabel("differnece of min and max of true value of the surroundings") plt.ylabel("differnece of min and max of true value of the surroundings") plt.show() plt.show() ``` ``` %% Output %% Output %% Cell type:code id:58da6063 tags: %% Cell type:code id:58da6063 tags: ``` python ``` python image = Image.open(images[0]) #Open the image and read it as an Image object image = Image.open(images[0]) #Open the image and read it as an Image object image = np.array(image)[1:,:] image = np.array(image)[1:,:] #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) z = np.array([22554,22552,22519,22561]) z = np.array([22554,22552,22519,22561]) print(z) print(z) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) a,b,c = np.linalg.solve(A,y)''' a,b,c = np.linalg.solve(A,y)''' A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(y) print(y) a,b,c = np.linalg.solve(A,y) a,b,c = np.linalg.solve(A,y) print(a,b,c) print(a,b,c) ``` ``` %% Output %% Output [22554 22552 22519 22561] [22554 22552 22519 22561] [-22596 67625 -90186] [-22596 67625 -90186] -17.49999999999879 -1.8333333333369712 22543.500000000004 -17.49999999999879 -1.8333333333369712 22543.500000000004 %% Cell type:code id:2562feeb tags: %% Cell type:code id:2562feeb tags: ``` python ``` python i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 160.16666666662906 160.16666666662906 %% Cell type:code id:470cc137 tags: %% Cell type:code id:470cc137 tags: ``` python ``` python a = 0 a = 0 b = 2 b = 2 c = 2 c = 2 i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 2032748510 2032748510 %% Cell type:code id:3292b395 tags: %% Cell type:code id:3292b395 tags: ``` python ``` python z = np.hstack((image[0,:3], image[1,0])) z = np.hstack((image[0,:3], image[1,0])) x = np.array([-1,0,1,-1]) x = np.array([-1,0,1,-1]) y = np.array([-1,-1,-1,0]) y = np.array([-1,-1,-1,0]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) ``` ``` %% Output %% Output -75749.00000000001 -75749.00000000001 C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) %% Cell type:code id:f9687830 tags: %% Cell type:code id:f9687830 tags: ``` python ``` python 0.5**2 + 1.5**2 0.5**2 + 1.5**2 ``` ``` %% Cell type:code id:e98eed4b tags: %% Cell type:code id:e98eed4b tags: ``` python ``` python ``` ```
prediction_MSE.ipynb +1 −1 Original line number Original line Diff line number Diff line %% Cell type:code id:dbef8759 tags: %% Cell type:code id:dbef8759 tags: ``` python ``` python import numpy as np import numpy as np from matplotlib import pyplot as plt from matplotlib import pyplot as plt from itertools import product from itertools import product import os import os import sys import sys from PIL import Image from PIL import Image from scipy.optimize import minimize from scipy.optimize import minimize from time import time from time import time ``` ``` %% Cell type:code id:b7a550e0 tags: %% Cell type:code id:b7a550e0 tags: ``` python ``` python def file_extractor(dirname="images"): def file_extractor(dirname="images"): files = os.listdir(dirname) files = os.listdir(dirname) scenes = [] scenes = [] for file in files: for file in files: scenes.append(os.path.join(dirname, file)) scenes.append(os.path.join(dirname, file)) return scenes return scenes def image_extractor(scenes): def image_extractor(scenes): image_folder = [] image_folder = [] for scene in scenes: for scene in scenes: files = os.listdir(scene) files = os.listdir(scene) for file in files: for file in files: image_folder.append(os.path.join(scene, file)) image_folder.append(os.path.join(scene, file)) images = [] images = [] for folder in image_folder: for folder in image_folder: ims = os.listdir(folder) ims = os.listdir(folder) for im in ims: for im in ims: if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": if im[-4:] == ".jp4" or im[-7:] == "_6.tiff": continue continue else: else: images.append(os.path.join(folder, im)) images.append(os.path.join(folder, im)) return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor return images #returns a list of file paths to .tiff files in the specified directory given in file_extractor def im_distribution(images, num): def im_distribution(images, num): """ """ Function that extracts tiff files from specific cameras and returns a list of all Function that extracts tiff files from specific cameras and returns a list of all the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise the tiff files corresponding to that camera. i.e. all pictures labeled "_7.tiff" or otherwise specified camera numbers. specified camera numbers. Parameters: Parameters: images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but images (list): list of all tiff files, regardless of classification. This is NOT a list of directories but of specific tiff files that can be opened right away. This is the list that we iterate through and of specific tiff files that can be opened right away. This is the list that we iterate through and divide. divide. num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits num (str): a string designation for the camera number that we want to extract i.e. "14" for double digits of "_1" for single digits. of "_1" for single digits. Returns: Returns: tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted tiff (list): A list of tiff files that have the specified designation from num. They are the files extracted from the 'images' list that correspond to the given num. from the 'images' list that correspond to the given num. """ """ tiff = [] tiff = [] for im in images: for im in images: if im[-7:-5] == num: if im[-7:-5] == num: tiff.append(im) tiff.append(im) return tiff return tiff ``` ``` %% Cell type:code id:9ed20f84 tags: %% Cell type:code id:9ed20f84 tags: ``` python ``` python def plot_hist(tiff_list, i): def plot_hist(tiff_list, i): """ """ This function is the leftovers from the first attempt to plot histograms. This function is the leftovers from the first attempt to plot histograms. As it stands it needs some work in order to function again. We will As it stands it needs some work in order to function again. We will fix this later. 1/25/22 fix this later. 1/25/22 """ """ image = tiff_list[i] image = tiff_list[i] image = Image.open(image) #Open the image and read it as an Image object image = Image.open(image) #Open the image and read it as an Image object image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = np.array(image)[1:,:] #Convert to an array, leaving out the first row because the first row is just housekeeping data image = image.astype(int) image = image.astype(int) row, col = image.shape row, col = image.shape predict = np.empty([row,col]) # create a empty matrix to update prediction predict = np.empty([row,col]) # create a empty matrix to update prediction predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[0,:] = np.copy(image[0,:]) # keep the first row from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[:,0] = np.copy(image[:,0]) # keep the first columen from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[-1,:] = np.copy(image[-1,:]) # keep the first row from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image predict[:,-1] = np.copy(image[:,-1]) # keep the first columen from the image diff = np.empty([row,col]) diff = np.empty([row,col]) diff[0,:] = np.zeros(col) # keep the first row from the image diff[0,:] = np.zeros(col) # keep the first row from the image diff[:,0] = np.zeros(row) diff[:,0] = np.zeros(row) diff[-1,:] = np.zeros(col) # keep the first row from the image diff[-1,:] = np.zeros(col) # keep the first row from the image diff[:,-1] = np.zeros(row) diff[:,-1] = np.zeros(row) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) '''z0 = image[0:-2,0:-2] '''z0 = image[0:-2,0:-2] z1 = image[0:-2,1:-1] z1 = image[0:-2,1:-1] z2 = image[0:-2,2::] z2 = image[0:-2,2::] z3 = image[1:-1,0:-2] z3 = image[1:-1,0:-2] y0 = -z0+z2-z3 y0 = -z0+z2-z3 y1 = z0+z1+z2 y1 = z0+z1+z2 y2 = -z0-z1-z2-z3 y2 = -z0-z1-z2-z3 predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] predict = [np.linalg.solve(A,np.array([y0[r,c],y1[r,c],y2[r,c]]))[-1] for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] diff = [(np.max([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])-np.min([z0[r,c],z1[r,c],z2[r,c],z3[r,c]])) for r in range(0,row-2) for c in range(0,col-2)] ''' ''' for r in range(1,row-1): # loop through the rth row for r in range(1,row-1): # loop through the rth row for c in range(1,col-1): # loop through the cth column for c in range(1,col-1): # loop through the cth column actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) actual_surrounding = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) #z = np.array([int(image[r-1,c-1]), int(image[r-1,c]), int(image[r-1,c+1]), int(image[r,c-1])]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) z = np.array([image[r-1,c-1], image[r-1,c], image[r-1,c+1], image[r,c-1]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) predict[r,c] = np.linalg.solve(A,y)[-1] predict[r,c] = np.linalg.solve(A,y)[-1] diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) diff[r,c] = (np.max(actual_surrounding)-np.min(actual_surrounding)) predict = np.ravel(predict[1:-1,1:-1]) predict = np.ravel(predict[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) diff = np.ravel(diff[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) image = np.ravel(image[1:-1,1:-1]) return image, predict, diff return image, predict, diff ``` ``` %% Cell type:code id:8e3ef654 tags: %% Cell type:code id:8e3ef654 tags: ``` python ``` python scenes = file_extractor() scenes = file_extractor() images = image_extractor(scenes) images = image_extractor(scenes) num_images = im_distribution(images, "_9") num_images = im_distribution(images, "_9") error_mean = [] error_mean = [] error_mean1 = [] error_mean1 = [] diff_mean = [] diff_mean = [] times = [] times = [] times1 = [] times1 = [] all_error = [] all_error = [] for i in range(len(num_images)): for i in range(len(num_images)): """start1 = time() """start1 = time() image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") image_1, predict_1, difference_1, x_s_1 = plot_hist(num_images, i, "second") stop1 = time() stop1 = time() times1.append(stop1-start1) times1.append(stop1-start1) error1 = np.abs(image_1-predict_1) error1 = np.abs(image_1-predict_1) error_mean1.append(np.mean(np.ravel(error1)))""" error_mean1.append(np.mean(np.ravel(error1)))""" start = time() start = time() image, predict, difference = plot_hist(num_images, i) image, predict, difference = plot_hist(num_images, i) stop = time() stop = time() times.append(stop-start) times.append(stop-start) error = np.abs(image-predict) error = np.abs(image-predict) all_error.append(np.ravel(error)) all_error.append(np.ravel(error)) error_mean.append(np.mean(np.ravel(error))) error_mean.append(np.mean(np.ravel(error))) diff_mean.append(np.mean(np.ravel(difference))) diff_mean.append(np.mean(np.ravel(difference))) ``` ``` %% Cell type:code id:a51dcb6f tags: %% Cell type:code id:fa65dcd6 tags: ``` python ``` python print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Average Error First and Second Added: {np.mean(error_mean)}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Standard Deviaiton of Mean Errors: {np.sqrt(np.var(error_mean))}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Difference: {np.mean(diff_mean)}") print(f"Average Time per Image for First: {np.mean(times)}") print(f"Average Time per Image for First: {np.mean(times)}") ``` ``` %% Output %% Output Average Error First and Second Added: 20.017164930235467 Average Error First and Second Added: 20.017164930235467 Standard Deviaiton of Mean Errors: 0.16101183692474846 Standard Deviaiton of Mean Errors: 0.16101183692474846 Average Difference: 53.678648426455226 Average Difference: 53.678648426455226 Average Time per Image for First: 9.85209345817566 Average Time per Image for First: 9.85209345817566 %% Cell type:code id:dda442ae tags: %% Cell type:code id:dda442ae tags: ``` python ``` python fig = plt.figure(figsize = (10,10)) fig = plt.figure(figsize = (10,10)) ax = fig.add_subplot() ax = fig.add_subplot() x = np.abs(predict-image) x = np.abs(predict-image) y = diff y = diff plt.plot(x,y,'o',alpha = 0.2) plt.plot(x,y,'o',alpha = 0.2) plt.rcParams.update({'font.size': 20}) plt.rcParams.update({'font.size': 20}) plt.xlabel("differnece to the true value" ) plt.xlabel("differnece to the true value" ) plt.ylabel("differnece of min and max of true value of the surroundings") plt.ylabel("differnece of min and max of true value of the surroundings") plt.show() plt.show() ``` ``` %% Output %% Output %% Cell type:code id:58da6063 tags: %% Cell type:code id:58da6063 tags: ``` python ``` python image = Image.open(images[0]) #Open the image and read it as an Image object image = Image.open(images[0]) #Open the image and read it as an Image object image = np.array(image)[1:,:] image = np.array(image)[1:,:] #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) #z = np.array([image[1-1,1-1], image[1-1,1], image[1-1,1+1], image[1,1-1]]) z = np.array([22554,22552,22519,22561]) z = np.array([22554,22552,22519,22561]) print(z) print(z) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) '''A = np.array([[-3,0,1],[0,-3,3],[-1,-3,4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) a,b,c = np.linalg.solve(A,y)''' a,b,c = np.linalg.solve(A,y)''' A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) A = np.array([[3,0,-1],[0,3,3],[1,-3,-4]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([-z[0]+z[2]-z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(y) print(y) a,b,c = np.linalg.solve(A,y) a,b,c = np.linalg.solve(A,y) print(a,b,c) print(a,b,c) ``` ``` %% Output %% Output [22554 22552 22519 22561] [22554 22552 22519 22561] [-22596 67625 -90186] [-22596 67625 -90186] -17.49999999999879 -1.8333333333369712 22543.500000000004 -17.49999999999879 -1.8333333333369712 22543.500000000004 %% Cell type:code id:2562feeb tags: %% Cell type:code id:2562feeb tags: ``` python ``` python i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 160.16666666662906 160.16666666662906 %% Cell type:code id:470cc137 tags: %% Cell type:code id:470cc137 tags: ``` python ``` python a = 0 a = 0 b = 2 b = 2 c = 2 c = 2 i0 = (a*(-1) + b*(1) + c) i0 = (a*(-1) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i1 = (a*(0) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i2 = (a*(1) + b*(1) + c) i3 = (a*(-1) + b*(0) + c) i3 = (a*(-1) + b*(0) + c) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) print(sum([(i0-z[0])**2,(i1-z[1])**2,(i2-z[2])**2,(i3-z[3])**2])) ``` ``` %% Output %% Output 2032748510 2032748510 %% Cell type:code id:3292b395 tags: %% Cell type:code id:3292b395 tags: ``` python ``` python z = np.hstack((image[0,:3], image[1,0])) z = np.hstack((image[0,:3], image[1,0])) x = np.array([-1,0,1,-1]) x = np.array([-1,0,1,-1]) y = np.array([-1,-1,-1,0]) y = np.array([-1,-1,-1,0]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) A = np.array([[-3,0,1],[0,-3,3],[1,3,-4]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) print(np.linalg.solve(A,y)[-1]) print(np.linalg.solve(A,y)[-1]) ``` ``` %% Output %% Output -75749.00000000001 -75749.00000000001 C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars C:\Users\calle\AppData\Local\Temp/ipykernel_15648/1729129504.py:5: RuntimeWarning: overflow encountered in ushort_scalars y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) y = np.array([z[0]-z[2]+z[3], z[0]+z[1]+z[2], -z[0]-z[1]-z[2]-z[3]]) %% Cell type:code id:f9687830 tags: %% Cell type:code id:f9687830 tags: ``` python ``` python 0.5**2 + 1.5**2 0.5**2 + 1.5**2 ``` ``` %% Cell type:code id:e98eed4b tags: %% Cell type:code id:e98eed4b tags: ``` python ``` python ``` ```