def taskC(): im = Image.open('checkerboard.png') im = np.array(im) x = [3, 5, 7, 11, 13, 15] y = [] for i in x: conv2D = Conv2D(3, 3, i, 1, 'rand', 9) print("Kernel Size: " + str(i)) [num_ops, _] = conv2D.forward(im) y.append(num_ops) print("num ops for kernel size of " + str(i) + ": " + str(num_ops)) plt.xlabel('Output Channel Size') plt.ylabel('Elapsed Time (s)') plt.title('Task B') plt.grid(True) plt.plot(x, y) plt.savefig('taskC_chart2.png')
def partB(img_name): # 3,2^i, 3, 5, rand img = cv2.imread(img_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) times = [] for i in xrange(0, 11): print i, ' in part B.' s_time = time.time() o_channel = 2**i conv2d = Conv2D(3, o_channel, 3, 1, 'rand') ops, conv_img = conv2d.forward(img) e_time = time.time() times.append(e_time - s_time) plt.xlabel('log(o_channels)') plt.ylabel('time') plt.title("partB time--o_channel graphs") plt.plot([i for i in xrange(11)], times, marker='x', color='red', label=img_name) plt.legend() plt.savefig('partB_' + img_name) plt.cla()
def main(): t = 1 #select Task if (t == 1): in_channel = 3 o_channel = 1 kernel_size = 3 stride = 1 mode = 'known' elif (t == 2): in_channel = 3 o_channel = 2 kernel_size = 5 stride = 1 mode = 'known' elif (t == 3): in_channel = 3 o_channel = 3 kernel_size = 3 stride = 2 mode = 'known' conv2D = Conv2D(in_channel, o_channel, kernel_size, stride) ######################### read image ################################# a = cv2.imread("1280x720.jpg") #print(len(a), len(a[0]), len(a[0][0])) #a = numpy.ndarray.reshape(a, (3, 1920, 1080)) a = numpy.ndarray.reshape(a, (3, 1280, 720)) #print(len(a), len(a[0]), len(a[0][0])) ############################## [ops, d] = conv2D.forward(a) print(ops, d)
def create_model_conv(): model = [] model.append(Conv2D(1, 10, (5, 5), (1, 1), (0, 0))) # [b,32,26,26] # model.append(Sigmoid()) # model.append(LeakyRelu()) model.append(MaxPool2D((2, 2), (2, 2), (0, 0))) # [b,64,12,12] model.append(Relu()) model.append(Conv2D(10, 20, (5, 5), (1, 1), (0, 0))) # [b,64,24,24] # model.append(Sigmoid()) # model.append(LeakyRelu()) model.append(MaxPool2D((2, 2), (2, 2), (0, 0))) # [b,64,12,12] model.append(Relu()) model.append(Flatten()) # [b,64*12*12] model.append(Linear(320, 50)) # model.append(LeakyRelu()) model.append(Relu()) # model.append(Sigmoid()) model.append(Linear(50, 10)) # [b,10] model.append(Softmax()) return model
def partA(img_name, i_channel, o_channel, kernel_size, stride, mode): img = cv2.imread(img_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) conv2d = Conv2D(i_channel, o_channel, kernel_size, stride, mode) ops, conv_img = conv2d.forward(img) # rescale the conv_img from tensor to imgs channel, height, width = conv_img.shape print channel for c in xrange(channel): res = conv_img[c].numpy() min_p = np.min(res) max_p = np.max(res) res = np.uint8((res - min_p) / (max_p - min_p) * 255) # res_trees_1_1.png cv2.imwrite( 'res_' + img_name[:-4] + '_ ' + str(o_channel) + '_' + str(c) + '.png', res)
def partC(img_name): kernel_sizes = [3, 5, 7, 9, 11] ops = [] img = cv2.imread(img_name) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for ks in kernel_sizes: print ks, 'in part C' conv2d = Conv2D(3, 2, ks, 1, 'rand') op, conv_img = conv2d.forward(img) print op ops.append(op) plt.xlabel('kernel size') plt.ylabel('the number of ops') plt.title('partC number of ops--kernel size') plt.plot([i for i in xrange(3, 12, 2)], ops, marker='x', color='red', label=img_name) plt.legend() plt.savefig('partC_' + img_name) plt.cla()
def taskB(): im = Image.open('checkerboard.png') im = np.array(im) x = np.arange(1, 10) y = [] for i in range(1, 10): conv2D = Conv2D(3, i, 3, 1, 'rand', 8) start = time.time() conv2D.forward(im) end = time.time() y.append(end-start) print("time for " + str(i) + " channels: " + str(end - start)) plt.xlabel('Kernel Size') plt.ylabel('Number of operations') plt.title('Task B') plt.grid(True) plt.plot(x, y) plt.savefig('taskB_chart2.png')
Created on Mon Aug 28 01:39:47 2017 @author: Bowen Wei """ #%% import numpy as np from scipy import misc from conv import Conv2D import time import matplotlib.pyplot as plt #%% input_image = misc.imread('small image.png') #%% conv2d = Conv2D(in_channel=3, o_channel=2, kernel_size=3, stride=1, mode='rand') #%% start = time.clock() [num_operation, output_image] = conv2d.forward(input_image) time_taken = time.clock() - start #%% misc.imsave('image2_task1.jpg', output_image[:, :, 0]) #%% num = np.zeros(5, dtype=np.int) n = 0 for k in [3, 5, 7, 9, 11]: conv2d = Conv2D(in_channel=3, o_channel=2, kernel_size=k,
from PIL import Image from conv import Conv2D import numpy as numpy import torch imageOne = './images/image01.jpg' imageTwo = './images/image02.jpg' def image_to_array(image_path): img = Image.open(image_path) img.load() img_array = numpy.asarray(img) print(img_array.shape) return img_array img_array1 = image_to_array(imageOne) img_tensor1 = torch.from_numpy(img_array1) img_array2 = image_to_array(imageTwo) img_tensor2 = torch.from_numpy(img_array2) task1 = Conv2D(3, 1, 3, 1, 'known') task3 = Conv2D(3, 3, 3, 2, 'known') task2 = Conv2D(3, 2, 5, 1, 'known') task1.forward(img_tensor1) task3.forward(img_tensor1) task2.forward(img_tensor1) task1.forward(img_tensor2) task3.forward(img_tensor2) task2.forward(img_tensor2)
import matplotlib.pyplot as plt import time from conv import Conv2D # convert PIL to tensor pil2tensor = transforms.ToTensor() tensor2pil = transforms.ToPILImage() img_pil = Image.open('1280x720.jpg') img = pil2tensor(img_pil) # convert JpegImageFile object to tensor # task1 conv2d = Conv2D(in_channel=3, o_channel=1, kernel_size=3, stride=1, mode='known') [numOperates, outImg] = conv2d.forward(img) print(numOperates) torchvision.utils.save_image(outImg, 'task1_1280x720.jpg', padding=0, normalize=True) if False: # task2 conv2d = Conv2D(in_channel=3, o_channel=2, kernel_size=5, stride=1,
from conv import Conv2D # import numpy as np import matplotlib.image as img # import matplotlib.pyplot as plt from PIL import Image if __name__ == "__main__": input_image_name = "cb.jpg" input_image = img.imread(input_image_name) # ==================Part A=================== conv2d = Conv2D(3, 2, 3, 1) number_of_ops, output_image = conv2d.forward(input_image) output_image_rows, output_image_columns, output_image_channels = output_image.shape # for i in range(output_image_channels): # # Normalize image to 0 1, and then multiply by 255 to get back the image # I8 = (((output_image[:, :, i] - output_image[:, :, i].min()) / (output_image[:, :, i].max() - output_image[:, :, i].min())) * 255.0).astype(np.uint8) # grayscale_image = Image.fromarray(I8) # output_image_name = str(i + 1) + ".jpg" # grayscale_image.save(output_image_name) print(number_of_ops) # # ==================Part B================== # for i in range(10): # import math # conv2d = Conv2D(3, int(math.pow(2, i)), 3, 1, 'rand') # import timeit # start_time = timeit.default_timer() # # code you want to evaluate
for i in range(im_no): img_name = input_image_loc + str('image_%i.jpg' % (i + 1)) curr_img = cv2.imread(img_name, cv2.IMREAD_COLOR) image_list.append(curr_img) curr_no = 1 task_no = 1 for current_image in image_list: print '\nImage No: ', curr_no # Part A-------------------------------------------------------------------- print '\nPART A: 2D Convolution' # Task 1 conv2d = Conv2D(3, 1, 3, 1, 'known') [op_count, float_tensor] = conv2d.forward(current_image) for i in range(len(float_tensor)): result_file = result_loc + str('image_%i_task_%i_ch_%i.jpg' % (curr_no, task_no, i)) cv2.imwrite(result_file, float_tensor[i]) print '\nOutput Channel:\t', i, '\t Result saved at:\t', result_file print 'Task 1 completed ...' task_no += 1 # Task 2 conv2d = Conv2D(3, 2, 5, 1, 'known') [op_count, float_tensor] = conv2d.forward(current_image)
try: img0 = Image.open('checkerboard.png') #img1 = Image.open('1280x720.jpg') img1 = Image.open('Current_Disney_logo.png') except IOError: pass imgToTensor = transforms.Compose([transforms.ToTensor()]) checkerboard = imgToTensor(img0) #(imgToTensor(img1),imgToTensor(img2)) colorImage = imgToTensor(img1) # Task 1 o_channel = 1 conv2d = Conv2D(in_channel=1, o_channel=1, kernel_size=3, stride=1, mode='known', task=1) [Number_of_ops, output_image] = conv2d.forward(checkerboard) print('num of ops for Task 1 is ', Number_of_ops) output_image = output_image.numpy().astype(np.uint8) im = Image.fromarray(output_image) im.save('task1.jpg') # Task 2 o_channel = 1 conv2d = Conv2D(in_channel=1, o_channel=1, kernel_size=3, stride=1,
def save_timg(tensor, filename): npimg = tensor.numpy() im = Image.fromarray(npimg.astype('uint8')) im.save(filename) device = torch.device("cuda:1,2,3" if torch.cuda.is_available() else "cpu") read_img1 = Image.open('1280.jpg') read_img2 = Image.open('1920.jpg') input_img1 = torch.from_numpy(np.asarray(read_img1)).to(device) input_img2 = torch.from_numpy(np.asarray(read_img2)).to(device) # Part A # init task 1 conv2d = Conv2D(3, 1, 3, 1, 'known') op_a1, out_1 = conv2d.forward(input_img1) save_timg(out_1[:, :, 0], 'a1.jpg') op_a2, out_2 = conv2d.forward(input_img2) save_timg(out_2[:, :, 0], 'a2.jpg') # task 2 conv2d = Conv2D(3, 2, 5, 1, 'known') op_a3, out_3 = conv2d.forward(input_img1) save_timg(out_3[:, :, 0], 'a3.jpg') save_timg(out_3[:, :, 1], 'a4.jpg') op_a4, out_4 = conv2d.forward(input_img2) save_timg(out_4[:, :, 0], 'a5.jpg') save_timg(out_4[:, :, 1], 'a6.jpg') # task 3 conv2d = Conv2D(3, 3, 3, 2, 'known') op_a5, out_5 = conv2d.forward(input_img1)
import numpy as np import matplotlib.pyplot as plt from scipy import misc from conv import Conv2D import time #%% # Part A ImageTitle = 'rock' # summer or rock or bob loadimage = misc.imread(ImageTitle + '.jpg') in_channel = 3 o_channel = 1 kernel_size = 3 stride = 1 mode = 'known' conv2d = Conv2D(in_channel, o_channel, kernel_size, stride, mode) [IteNumber, ConvImage] = conv2d.forward(loadimage) ConvImageShape = ConvImage.shape for ImNum in range(0, ConvImageShape[2]): misc.imsave( ImageTitle + '_ochannel_' + str(o_channel) + '_ksize_' + str(kernel_size) + '_stride_' + str(stride) + '_' + str(ImNum) + '_' + '.jpg', ConvImage[:, :, ImNum]) #%% # Part B ImageTitle = 'bob' loadimage = misc.imread(ImageTitle + '.jpg') in_channel = 3 kernel_size = 3 stride = 1
import matplotlib.pyplot as plt import scipy.misc import time import numpy as np img = cv2.imread('pic1.jpg') #change to pic2 to run for the second picture img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #plt.imshow(img) img = img.astype(float) tensor_img = torch.from_numpy(img) tensor_img = tensor_img.type(torch.FloatTensor) ##### PART A ##### uncomment if you want to run, comment partB and partC conv2d = Conv2D(3, 1, 3, 1, 'known') [num_of_operation, output_image] = conv2d.forward(tensor_img) for i in range(0, output_image.size(2)): result = output_image[:, :, i].numpy() scipy.misc.imsave("Pic1_Task_1_" + str(i + 1) + ".jpg", result) ##### PART B ##### uncomment if you want to run, comment partA and partC '''TotalTime = np.zeros(shape = (11,1)) I = np.zeros(shape = (11,1)) for i in range(0,10): start_time = time.time() conv2d = Conv2D(3,2**i,3,1,'known') [num_of_operation, output_image] = conv2d.forward(tensor_img) end_time = time.time() TotalTime[i] = end_time-start_time
import numpy as np import cv2 img = cv2.imread('image1.jpg', -1) #img = cv2.imread('image2.jpg',-1) # Part A from conv import Conv2D conv2d = Conv2D(3, 1, 3, 1, '') #conv2d = Conv2D(3,2,5,1,'') #conv2d = Conv2D(3,3,3,2,'') count, output_image = conv2d.forward(img) # Part B for i in range(10): import math from conv import Conv2D conv2d = Conv2D(3, int(math.pow(2, i)), 3, 1, 'rand') import timeit start = timeit.default_timer() count, output_image = conv2d.forward(img) stop = timeit.default_timer() run_time = stop - start print(i, run_time) # Part C for i in range(1, 6): import math from conv import Conv2D conv2d = Conv2D(3, 2, 2 * i + 1, 1, 'rand') count, output_image = conv2d.forward(img)
s_img = "1280_720" l_img = "1920_1080" #t_img = "100_100" imgSet = [s_img, l_img] #imgSet = [t_img] for img in imgSet: PIL_img = Image.open(img + ".jpg") ToTensor = transforms.ToTensor() ToImage = transforms.ToPILImage() input_FT = ToTensor(PIL_img) # Part A # Task 1 convA1 = Conv2D(3, 1, 3, 1, "known") no_opsA1, o_FTA1 = convA1.forward(input_FT) o_imgA1 = ToImage(o_FTA1) o_imgA1.save(img + "PartA_Task1.jpg") print(img, "Number of Operations of Task 1: ", no_opsA1) # Task 2 convA2 = Conv2D(3, 2, 5, 1, "known") no_opsA2, o_FTA2 = convA2.forward(input_FT) for chan in range(2): o_imgA2 = ToImage(o_FTA2[chan].unsqueeze(0)) o_imgA2.save(img + "Channel " + str(chan) + "_PartA_Task2.jpg") print(img, "Number of Operations of Task 2: ", no_opsA2) # Task 3 convA3 = Conv2D(3, 3, 3, 2, "known")
import timeit import matplotlib.pyplot as plt # Loading Images path1 = "./img1.jpg" image1 = cv2.imread(path1) C1 = image1[:, :, 0] C2 = image1[:, :, 1] C3 = image1[:, :, 2] image = np.zeros((3, image1.shape[0], image1.shape[1]), dtype=float) image[0, :, :] = C1 image[1, :, :] = C2 image[2, :, :] = C3 o_channel = 3 print(image.shape) # Part-A conv2D = Conv2D(3, o_channel, 3, 2, 'known') no_ops, output_temp = conv2D.forward(image) output = output_temp.numpy() print(no_ops) for i in range(o_channel): cv2.imwrite("Task3_img1_channel_" + str(i) + ".jpg", output[i, :, :]) # Part- B L_channels = np.asarray([2**i for i in range(5)]) L_time = np.zeros((5, 1), dtype='float32') for i in range(len(L_channels)): #L_channels[i]=2**i o_channel = L_channels[i] conv2D = Conv2D(3, o_channel, 3, 1, 'rand') start_time = timeit.default_timer() no_ops, output_temp = conv2D.forward(image)
def normalize(filtered_image): return (filtered_image[:, :] - filtered_image[:, :].min() ) / filtered_image.ptp() # value - min/max-min ## Read Image as a NUMPY array input_image_1 = color.rgb2gray(io.imread('cartoon.jpg')) input_image_2 = color.rgb2gray(io.imread('checker.jpg')) ##convert to TORCH TENSOR. t_input_image_1 = torch.from_numpy(input_image_1) t_input_image_2 = torch.from_numpy(input_image_2) ############################# PART A ############################## # ## TASK #1 conv = Conv2D(in_channel=3, o_channel=1, kernel_size=3, stride=1, mode='known') (operation_count, filtered_image) = conv.forward(t_input_image_1) print "Operation Count: " + str(operation_count) f_image = Image.fromarray(filtered_image.numpy() * 255).convert('RGB') # f_image.save('output_cartoon_k1_task_1.png') f_image.show() (operation_count, filtered_image) = conv.forward(t_input_image_2) print "Operation Count: " + str(operation_count) f_image = Image.fromarray(filtered_image.numpy() * 255).convert('RGB') # f_image.save('output_checker_k1_task_1.png') f_image.show() #TASK #2 conv = Conv2D(in_channel=3, o_channel=2, kernel_size=5, stride=1, mode='known') (operation_count, filtered_images) = conv.forward(t_input_image_2)
print("Usage: python main.py part[A, B, C]") exit(1) tasks = [[3, 1, 3, 1], [3, 2, 5, 1], [3, 3, 3, 2]] if argv[1] == 'partA': # Part A for tsk_id in range(len(tasks)): task = tasks[tsk_id] print('Part A Task ' + str(tsk_id + 1)) for img_id in range(len(img_tensors)): print("Image ", img_id, "size: ", img_tensors[img_id].size()) conv = Conv2D( task[0], task[1], task[2], task[3], ) ops, output_img = conv.forward(img_tensors[img_id]) print('Total operation', ops, ', output tensor size:', output_img.size()) num_channels = output_img.size()[0] if num_channels == 1: # task 1 file_name = "image" + str(img_id) + "/plt_" + str( img_id) + "_partA_task" + str(tsk_id + 1) + "_k1.jpg" print("Save to", file_name, "\n") save_image(output_img, file_name, normalize=True) elif num_channels == 2: # task 2