-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualize_trace.py
More file actions
136 lines (116 loc) · 3.74 KB
/
visualize_trace.py
File metadata and controls
136 lines (116 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import numpy as np
import pandas as pd
from functools import reduce
import operator
import torch
import os
import json
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import colors
ACTION = [
"clockwise_rotate",
"counterclockwise_rotate",
"horizontal_flip",
"vertical_flip",
"edit",
"select",
"move_left",
"move_right",
"move_up",
"move_down",
"none"
]
# 0~9까지는 색깔
# 10~19까지는 select된 색깔
def plot_one(ax, i):
cmap = colors.ListedColormap(
[
'#000000', # 0 검은색
'#0074D9', # 1 파란색
'#FF4136', # 2 빨간색
'#2ECC40', # 3 초록색
'#FFDC00', # 4 노란색
'#AAAAAA', # 5 회색
'#F012BE', # 6 핑크색
'#FF851B', # 7 주황색
'#7FDBFF', # 8 하늘색
'#870C25', # 9 적갈색
])
#norm = colors.Normalize(vmin=0, vmax=9)
norm = colors.Normalize(vmin=0, vmax=19)
input_matrix = task["step"][i]
ax.imshow(input_matrix, cmap=cmap, norm=norm)
ax.grid(True,which='both',color='lightgrey', linewidth=0.5)
ax.set_yticks([x-0.5 for x in range(1+len(input_matrix))])
ax.set_xticks([x-0.5 for x in range(1+len(input_matrix[0]))])
ax.set_xticklabels([])
ax.set_yticklabels([])
if(i==0):
ax.set_title('input')
elif(i==len(task["step"])-1):
ax.set_title('output' + ' (' + task["step_function"][i-1]+')')
else:
ax.set_title('step: '+ str(i)+' ('+task["step_function"][i-1]+')')
def plot_task(task):
"""
Plots the first train and test pairs of a specified task,
using same color scheme as the ARC app
"""
num_step = len(task["step"])
fig, axs = plt.subplots(1, (num_step), figsize=(3*(num_step), 3*2))
for i in range(num_step):
plot_one(axs[i], i)
plt.tight_layout()
plt.show()
# Select json file
data_path = Path('/home/jovyan/workspace/Jaehyun/Mini_ARC_DT/')
training_path = data_path / 'mini_arc_train'
training_tasks = sorted(os.listdir(training_path))
# Show training set
for i in range(len(training_tasks)):
task_file = str(training_path / training_tasks[i])
with open(task_file, 'r') as f:
task = json.load(f)
#print("#{0}: {1}".format(i, training_tasks[i]))
#plot_task(task)
#print("1st grid: {0}".format(task["action_sequence"]["action_sequence"][:]["grid"]))
#print("1st action: {0}".format(task["action_sequence"]["action_sequence"][0]))
step_gap = 4
total_step = len(task["action_sequence"]["action_sequence"])
start_idx = np.random.randint(0, total_step-step_gap+1)
actions_type = {
"start":0,
"undo":1,
"edit":2,
"copyFromInput":3,
"rotate":4,
"reflectY":5,
"reflectX":6,
"translate":7,
"resizeOutputGrid":8,
"resetOutputGrid":9,
"selected_cells":10
}
actions_num = len(actions_type)
states = []
actions_str = []
actions = []
rtgs = []
timesteps = []
for i in range(start_idx, start_idx+step_gap):
states.append(reduce(operator.add, task["action_sequence"]["action_sequence"][i]["grid"]))
actions_str.append(task["action_sequence"]["action_sequence"][i]["action"]["tool"])
actions.append(actions_type[task["action_sequence"]["action_sequence"][i]["action"]["tool"]])
rtgs.append(float(i)/step_gap)
timesteps.append(i)
states = torch.IntTensor(states)
actions = torch.IntTensor(actions)
rtgs = torch.FloatTensor(rtgs)
timesteps = torch.IntTensor(timesteps)
print(states)
print(actions)
print(actions_str)
print(rtgs)
print(timesteps)
#print(states.shape)