def run_pretrained(input_state,model,action_states,gameState):
print '\n\nLoading pretrained weights onto model...'
model.load_weights(p.PRETRAINED_PATH)
epsilon=1
while True:
print 'Running pretrained model (no exploration) with weights at ', p.PRETRAINED_PATH
nn_out = model.predict(input_state,batch_size=1,verbose=0)
nn_action = [[0,0]]
nn_action[0][np.argmax(nn_out)] =1
action,rand_flag = select_action(nn_action+action_states,prob=[epsilon,(1-epsilon)*1/7,(1-epsilon)*6/7])
rgbDisplay, reward, tState = gameState.frame_step(action)
grayDisplay = (np.dot(np.fliplr(imrotate(imresize(rgbDisplay, (80,80), interp='bilinear'), -90))[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
output_state = np.append(grayDisplay,input_state[:,:p.HISTORY-1,:,:], axis=1)
#############################################################################################################################################################################
python类imrotate()的实例源码
def read_images_from_disk(input_queue):
"""Consumes a single filename and label as a ' '-delimited string.
Args:
filename_and_label_tensor: A scalar string tensor.
Returns:
Two tensors: the decoded image, and the string label.
"""
label = input_queue[1]
file_contents = tf.read_file(input_queue[0])
example = tf.image.decode_png(file_contents, channels=3)
return example, label
# def random_rotate_image(image):
# angle = np.random.uniform(low=-10.0, high=10.0)
# return misc.imrotate(image, angle, 'bicubic')
def crawl_directory(directory, augment_with_rotations=False, first_label=0):
"""Crawls data directory and returns stuff."""
label_idx = first_label
images = []
labels = []
info = []
# traverse root directory
for root, _, files in os.walk(directory):
logging.info('Reading files from %s', root)
for file_name in files:
full_file_name = os.path.join(root, file_name)
img = imread(full_file_name, flatten=True)
for idx, angle in enumerate([0, 90, 180, 270]):
if not augment_with_rotations and idx > 0:
break
images.append(imrotate(img, angle))
labels.append(label_idx + idx)
info.append(full_file_name)
if len(files) == 20:
label_idx += 4 if augment_with_rotations else 1
return images, labels, info
def _rotate_batch(batch, angle):
return np.vstack([rotate(x_i.reshape(28, 28), angle).reshape([-1, 28*28])
for x_i in batch])
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def random_rotate_image(image):
# rotate image for data augmentation
angle = np.random.uniform(low=-20.0, high=20.0)
return misc.imrotate(image, angle, 'bicubic')
def plotCluster( _x, labels, core_samples_mask, n_clusters_, f):
unique_labels = set(labels)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels)))
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = 'k'
class_member_mask = (labels == k)
xy = _x[class_member_mask & ~core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=6)
xy = _x[class_member_mask & core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
#plt.title('Estimated number of clusters: %d' % n_clusters_)
#plt.axis('off')
#misc.imrotate(ax, 270)
index = f
print f
plt.axis('off')
plt.savefig(f)
image_rotate(f)
plt.close()
#plt.show()
return
def plotCluster( _x, labels, core_samples_mask, n_clusters_, f):
unique_labels = set(labels)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels)))
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = 'k'
class_member_mask = (labels == k)
xy = _x[class_member_mask & ~core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=6)
xy = _x[class_member_mask & core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
#plt.title('Estimated number of clusters: %d' % n_clusters_)
#plt.axis('off')
#misc.imrotate(ax, 270)
index = f
print f
plt.axis('off')
plt.savefig(f)
image_rotate(f)
plt.close()
#plt.show()
return
def plotCluster(_x, labels, core_samples_mask, n_clusters_, f):
unique_labels = set(labels)
colors = plt.cm.Spectral(np.linspace(0, 1, len(unique_labels)))
for k, col in zip(unique_labels, colors):
if k == -1:
# Black used for noise.
col = 'k'
class_member_mask = (labels == k)
xy = _x[class_member_mask & ~core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=6)
xy = _x[class_member_mask & core_samples_mask]
ax = plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=col,
markeredgecolor='k', markersize=14)
#plt.title('Estimated number of clusters: %d' % n_clusters_)
#plt.axis('off')
#misc.imrotate(ax, 270)
index = f
print f
plt.axis('off')
plt.savefig(f)
image_rotate(f)
plt.close()
#plt.show()
return
##############################################################################################
# Rotating the image, as output was comming tilted
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def rotate_images(images, angle, image_size):
images_list = [None] * images.shape[0]
for i in range(images.shape[0]):
images_list[i] = misc.imrotate(images[i,:,:,:], angle)
images_rot = np.stack(images_list,axis=0)
sz1 = images_rot.shape[1]/2
sz2 = image_size/2
images_crop = images_rot[:,(sz1-sz2):(sz1+sz2),(sz1-sz2):(sz1+sz2),:]
return images_crop
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def run_pretrained(input_state,model,action_states,gameState):
print '\n\nLoading pretrained weights onto model...'
model.load_weights(p.PRETRAINED_PATH)
epsilon=1
while True:
print 'Running pretrained model (no exploration) with weights at ', p.PRETRAINED_PATH
nn_out = model.predict(input_state,batch_size=1,verbose=0)
nn_action = [[0,1]] if np.argmax(nn_out) else [[1,0]]
action,rand_flag = select_action(nn_action+action_states,prob=[epsilon,(1-epsilon)/2,(1-epsilon)/2])
rgbDisplay, reward, tState = gameState.frame_step(action)
#grayDisplay = (np.dot(imresize(rgbDisplay, (80,80), interp='bilinear')[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
grayDisplay = (np.dot(np.fliplr(imrotate(imresize(rgbDisplay, (80,80), interp='bilinear'), -90))[:,:,:3], [0.299, 0.587, 0.114])).reshape((1,1,80,80))
output_state = np.append(input_state[:,1:,:,:], grayDisplay,axis=1)
#############################################################################################################################################################################
def rotate_frame(frame, angle):
# Perform the image rotation and update the fits header
#frame[np.isnan(frame)] = 0.0
new_frame = ndimage.interpolation.rotate(frame, angle, reshape=False, order=1, mode='constant', cval=float('nan'))
#new_frame = misc.imrotate(frame, angle, interp="bilinear")
# Return the rotated frame
return new_frame
def rotate_frame(frame, angle):
# Perform the image rotation and update the fits header
#frame[np.isnan(frame)] = 0.0
new_frame = ndimage.interpolation.rotate(frame, angle, reshape=False, order=1, mode='constant', cval=float('nan'))
#new_frame = misc.imrotate(frame, angle, interp="bilinear")
# Return the rotated frame
return new_frame
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def rotate_images(images, angle, image_size):
images_list = [None] * images.shape[0]
for i in range(images.shape[0]):
images_list[i] = misc.imrotate(images[i,:,:,:], angle)
images_rot = np.stack(images_list,axis=0)
sz1 = images_rot.shape[1]/2
sz2 = image_size/2
images_crop = images_rot[:,(sz1-sz2):(sz1+sz2),(sz1-sz2):(sz1+sz2),:]
return images_crop
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def random_rotate_image(image):
angle = np.random.uniform(low=-10.0, high=10.0)
return misc.imrotate(image, angle, 'bicubic')
def _rotate_batch(batch, angle):
return np.vstack([rotate(x_i.reshape(28, 28), angle).reshape([-1, 28*28])
for x_i in batch]) / 255.