def ball_batch_imitate(initial_config, start_index, pic_path, file_path, batch_size, state_followed, image_followed, force_label, force_followed): ''' output: two images: past and now @image_followed: contain original image @state_following: state_input @force_followed: the regressed force (new force by network) ''' As = [] for it in range(start_index, start_index + batch_size): new_force_0 = force_followed[it - start_index] * force_scale new_force_0 = [round(i / 2) * 2 for i in new_force_0] old_pos = state_followed[it - start_index, 1, 0] * pos_scale new_pos_real = state_followed[it - start_index, 1, 1] * pos_scale old_vel = state_followed[it - start_index, 0, 0] * vel_scale # print(new_force_0) A, B, _ = simulation_func(initial_config, count=it * 2, iteration=1, pic_path=pic_path, file_path=file_path, new_force=new_force_0, is_newstate=True, new_pos=old_pos, new_vel=old_vel, force_func=generate_, verbose=False, iswrite=True, isdraw=False) new_pos_fake = B[1, 1] * pos_scale img_concat(image_followed[it - start_index, 1], A[1], path=pic_path, count=it - start_index, pos1=new_pos_real, pos2=new_pos_fake, ball_config=initial_config, force_real=force_label[it - start_index], force_fake=force_followed[it - start_index], is_force_line=False) As.append(A[1]) As = np.array(As) #32,256,256,3 return As
def ball_continuous_input(initial_config, total_step, start_index, pic_path, file_path, isdraw=False): ''' similar to ball_batch_input, except that a continuous process of actor movements, of steps of @total_step ''' As = [] Bs = [] Cs = [] new_pos_0 = generate_([i for i in range(ball_radius, 65 - ball_radius)]) new_vel_0 = generate_( [i for i in range(int(-1 * vel_scale), int(vel_scale) + 1)]) for it in range(total_step): new_force_0 = generate_( [i for i in range(int(-1 * force_scale), int(force_scale) + 1, 2)]) A, B, C = simulation_func(initial_config, count=it, iteration=1, pic_path=pic_path, file_path=file_path, new_force=new_force_0, is_newstate=True, new_pos=new_pos_0, new_vel=new_vel_0, verbose=False, iswrite=False, isdraw=isdraw) new_vel_0 = B[0, 1, :] * vel_scale new_pos_0 = B[1, 1, :] * pos_scale # print("new_vel: ",new_vel_0) As.append(A) Bs.append(B) Cs.append(C) # print("force_label:",C) As = np.array(As) #32,2,256,256,3 [pic0,pic1] Bs = np.array(Bs) #32,2,2,2 [[vel(0,1)],[pos(0,1)]] each is [x,y] Cs = np.array(Cs) #32,2 [Fx,Fy] return As, Bs, Cs
def ball_batch_input(initial_config, start_index, pic_path, file_path, batch_size): ''' single movement of actor in batch @start_index: the number of picture in direcotory while saving output: As batch_size,2,256,256,3 [pic0,pic1] Bs batch_size,2,2,2 [[vel(past,now)],[pos(past,now)]] each is [x,y] Cs batch_size,2 [Fx,Fy] ''' As = [] Bs = [] Cs = [] force_record = [] for it in range(start_index, start_index + batch_size): new_force_0 = generate_([i for i in range(-10, 11, 2)]) force_record.append(new_force_0) new_pos_0 = generate_( [i for i in range(ball_radius, 65 - ball_radius)]) new_vel_0 = generate_([i for i in range(-5, 6)]) A, B, C = simulation_func(initial_config, count=it * 2, iteration=1, pic_path=pic_path, file_path=file_path, new_force=new_force_0, is_newstate=True, new_pos=new_pos_0, new_vel=new_vel_0, verbose=False, iswrite=False, isdraw=False) As.append(A) Bs.append(B) Cs.append(C) As = np.array(As) #32,2,256,256,3 [pic0,pic1] Bs = np.array(Bs) #32,2,2,2 [[vel(0,1)],[pos(0,1)]] each is [x,y] Cs = np.array(Cs) #32,2 [Fx,Fy] return As, Bs, Cs
ball_config=initial_config, force_real=force_label_[it], force_fake=[0, 0], is_force_line=False, issave=False) image_record.append(img_result) # update the current state of imitator if (it < opt.total_step - 1): _, state_single_out, _ = simulation_func( initial_config, count=it, iteration=1, pic_path=opt.log_dir + '/images/imitator/' + str(epoch) + '_', file_path=opt.log_dir + '/inputs/imitator/' + str(epoch) + '_', new_force=[0, 0], is_newstate=True, new_pos=[32, 32], new_vel=starting_vel * vel_scale, force_func=generate_, verbose=False, iswrite=False, isdraw=False) starting_vel = state_single_out[0, 1] image_concat = np.array([step_image_o_e, image_input_[it + 1, 1] ])[np.newaxis, :] #1,2,256,256,3 print(epoch, loss_e, loss_dyn_e, loss_i_e) # draw the gif # create_gif_byreading(path=opt.log_dir+'/images/imitator/',epoch=epoch,total_step=opt.total_step, gif_name=opt.log_dir+'/images/imitator/gifs/'+str(epoch)+'.gif')