def main(): """ 불러온 이미지를 RGB 값을 갖는 3차원 벡터로 변환합니다. """ # 이미지를 불러옵니다. filenames = ["./data/aurora.jpg", "./data/raccoon.jpg"] img = Image.open(filenames[0]) img = img.convert("RGB") # 이미지를 NumPy 배열로 변환합니다. (높이, 넓이, 3) 차원의 행렬로 변환하여 돌려줍니다. image_vector = np.asarray(img) prep_image_vector = preprocess(image_vector) # K-means의 K값을 설정합니다. K = 32 new_image, clusters, centroids = kmeans(prep_image_vector, K) new_image = postprocess(new_image) # 변환된 벡터의 타입을 처리된 이미지와 같이 8bits로 설정합니다. new_image = new_image.astype("uint8") # 데이터를 이미지로 다시 변환합니다. new_img = Image.fromarray(new_image, "RGB") # 이미지를 저장하고 실행결과를 출력합니다. new_img.save("image1out.jpg") elice = elice_utils.EliceUtils() elice.send_image("image1out.jpg") # 점수를 확인합니다. print("Score = %.2f" % elice.calc_score(image_vector, new_image)) return
import matplotlib as mpl mpl.use("Agg") import matplotlib.pyplot as plt import elice_utils import numpy as np elice = elice_utils.EliceUtils() def circle(P): return np.linalg.norm(P) - 1 # 밑의 코드와 동일하게 동작합니다. # return np.sqrt(np.sum(P * P)) - 1 def diamond(P): return np.abs(P[0]) + np.abs(P[1]) - 1 def smile(P): def left_eye(P): eye_pos = P - np.array([-0.5, 0.5]) return np.sqrt(np.sum(eye_pos * eye_pos)) - 0.1 def right_eye(P): eye_pos = P - np.array([0.5, 0.5]) return np.sqrt(np.sum(eye_pos * eye_pos)) - 0.1 def mouth(P): if P[1] < 0: return np.sqrt(np.sum(P * P)) - 0.7 else: return 1
import matplotlib as mpl mpl.use("Agg") import matplotlib.pyplot as plt import numpy as np from matplotlib.animation import FuncAnimation import elice_utils as eu elice_utils = eu.EliceUtils() # 자료 읽어오는 함수 def read_data(): X, Y = [], [] with open("./data/input.txt") as f: for line in f: items = list(map(lambda x: float(x), line.split(","))) X.append(items[0:4]) Y.append(items[4]) X, Y = np.array(X), np.array(Y) return X, Y def loss(X, Y, W): """ Sum of Squared Error(SSE)를 기반으로 하여 손실함수를 구해봅시다. W는 A, B, C, D가 담겨있습니다. Y' = X.dot(W) 일 때, 손실값은 |Y - Y'|^2가 됩니다.