def plot_data(self): for i,dat in enumerate(self.data): plt.imshow(dat, cmap=cm.jet, interpolation=None, extent=[11,22,-3,2]) txt='plot '.format(self.n[i]) txt+='\nmin {0:.2f} und max {1:.2f}'.format(self.min[i],self.max[i]) txt+='\navg. min {0:.2f} und avg. max {1:.2f}'.format(mean(self.min),mean(self.max)) plt.suptitle(txt,x=0.5,y=0.98,ha='center',va='top',fontsize=10) plt.savefig(join(self.plotpath,'pic_oo_'+str(self.n[i])+'.png')) plt.close() # wichtig, sonst wird in den selben Plot immer mehr reingepackt
def update(dummy): if str(radio_buttons.value_selected) != labels.label: labels.label = radio_buttons.value_selected slider.valmax = len(labels[labels.label].samples) slider.val = labels[labels.label].index ax_slider.set_xlim(0, slider.valmax) labels[labels.label].index = int(slider.val) current = labels[labels.label].samples.iloc[labels[labels.label].index] with open(os.path.join(dirname, current['name']), 'rb') as f: patch = pickle.load(f) print(f.name) plt.suptitle(patch.name) vol = patch.volumetric[2:2 + 3].transpose(1, 2, 0) display_vol = 2 * np.arctan(vol) / np.pi radius = patch.rgb.shape[1] / 2 extent = (-radius, radius, -radius, radius) ax1.clear() ax2.clear() ax3.clear() ax1.imshow(patch.rgb.transpose(1, 2, 0), extent=extent) ax1.set_title('rgb') ax2.imshow(patch.rgb.transpose(1, 2, 0), extent=extent) ax2.imshow(display_vol, extent=extent, alpha=0.5) if patch.obb is not None: patch.obb.plot(ax2, lw=4, color='yellow') patch.obb.plot(ax2, lw=3, ls='--', color='red') ax2.set_title('both') ax3.imshow(display_vol, extent=extent) ax3.set_title('vol:max={:.1f}'.format(vol.max())) fig.canvas.draw_idle()
def train(n_epochs, _batch_size, start_epoch=0): """ train with fixed batch_size for given epochs make some example plots and save model after each epoch """ global batch_size batch_size = _batch_size # create a dataqueue with the keras facilities. this allows # to prepare the data in parallel to the training sample_dataqueue = GeneratorEnqueuer(generate_real_samples(batch_size), use_multiprocessing=True) sample_dataqueue.start(workers=2, max_queue_size=10) sample_gen = sample_dataqueue.get() # targets for loss function gan_sample_dataqueue = GeneratorEnqueuer( generate_latent_points_as_generator(batch_size), use_multiprocessing=True) gan_sample_dataqueue.start(workers=2, max_queue_size=10) gan_sample_gen = gan_sample_dataqueue.get() # targets for loss function valid = -np.ones((batch_size, 1)) fake = np.ones((batch_size, 1)) dummy = np.zeros((batch_size, 1)) # Dummy gt for gradient penalty bat_per_epo = int(n_samples / batch_size) # we need to call the discriminator once in order # to initialize the input shapes [X_real, cond_real] = next(sample_gen) latent = np.random.normal(size=(batch_size, latent_dim)) critic_model.predict([X_real, cond_real, latent]) for i in trange(n_epochs): epoch = 1 + i + start_epoch # enumerate batches over the training set for j in trange(bat_per_epo): for _ in range(n_disc): # fetch a batch from the queue [X_real, cond_real] = next(sample_gen) latent = np.random.normal(size=(batch_size, latent_dim)) d_loss = critic_model.train_on_batch( [X_real, cond_real, latent], [valid, fake, dummy]) # we get for losses back here. average, valid, fake, and gradient_penalty # we want the average of valid and fake d_loss = np.mean([d_loss[1], d_loss[2]]) # train generator # prepare points in latent space as input for the generator [latent, cond] = next(gan_sample_gen) # update the generator via the discriminator's error g_loss = generator_model.train_on_batch([latent, cond], valid) # summarize loss on this batch print(f'{epoch}, {j + 1}/{bat_per_epo}, d_loss {d_loss}' + \ f' g:{g_loss} ') # , d_fake:{d_loss_fake} d_real:{d_loss_real}') if np.isnan(g_loss) or np.isnan(d_loss): raise ValueError('encountered nan in g_loss and/or d_loss') hist['d_loss'].append(d_loss) hist['g_loss'].append(g_loss) # plot generated examples plt.figure(figsize=(25, 25)) n_plot = 30 X_fake, cond_fake = generate_fake_samples(n_plot) for iplot in range(n_plot): plt.subplot(n_plot, 25, iplot * 25 + 1) plt.imshow(cond_fake[iplot, :, :].squeeze(), cmap=plt.cm.gist_earth_r, norm=LogNorm(vmin=0.01, vmax=1)) plt.axis('off') for jplot in range(1, 24): plt.subplot(n_plot, 25, iplot * 25 + jplot + 1) plt.imshow(X_fake[iplot, jplot, :, :].squeeze(), vmin=0, vmax=1, cmap=plt.cm.hot_r) plt.axis('off') plt.colorbar() plt.suptitle(f'epoch {epoch:04d}') plt.savefig( f'{plotdir}/fake_samples_{params}_{epoch:04d}_{j:06d}.{plot_format}' ) # plot loss plt.figure() plt.plot(hist['d_loss'], label='d_loss') plt.plot(hist['g_loss'], label='g_loss') plt.ylabel('batch') plt.legend() plt.savefig(f'{plotdir}/training_loss_{params}.{plot_format}') pd.DataFrame(hist).to_csv('hist.csv') plt.close('all') generator.save(f'{outdir}/gen_{params}_{epoch:04d}.h5') critic.save(f'{outdir}/disc_{params}_{epoch:04d}.h5')
plt.subplot(241).set_title("无翻拍 : %s 分" % score_1) plt.imshow(image_1) plt.axis('off') plt.subplot(242).set_title("翻拍 : %s 分" % score_2) plt.imshow(image_2) plt.axis('off') plt.subplot(243).set_title("无翻拍 : %s 分" % score_3) plt.imshow(image_3) plt.axis('off') plt.subplot(244).set_title("无翻拍-黑白 : %s 分" % score_4) plt.imshow(image_4) plt.axis('off') plt.subplot(245).set_title("电脑拍摄 : %s 分" % score_5) plt.imshow(image_5) plt.axis('off') plt.subplot(246).set_title("手机拍摄 : %s 分" % score_6) plt.imshow(image_6) plt.axis('off') plt.subplot(247).set_title("电脑拍摄-翻拍 : %s 分" % score_7) plt.imshow(image_7) plt.axis('off') plt.subplot(248).set_title("手机拍摄-翻拍 : %s 分" % score_8) plt.imshow(image_8) plt.axis('off') plt.subplots_adjust(left=0.1, right=0.9, top=0.9, bottom=0.1) plt.axis('off') plt.suptitle("腾讯-活体检测") plt.show()
def plot_feature_space(self, m=0, n=1, c=None, r=300, hold=False, xlim=None, ylim=None, xtpl='MNF %d', ytpl='MNF %d', alpha=0.5, stitle='MNF Feature Space: Axes %d and %d', interact=False): ''' Create a 2D projection of the feature space and display it. ''' self.__dims__ = (m, n, c) # Remember these indices # Create a new figure of size 9x9 points, using 72 dots per inch fig = figure(figsize=self.size, dpi=self.dpi) self.ax = fig.add_subplot(111) defaults = { 'linewidths': (0, ), 's': (30, ), 'cmap': 'YlGnBu', 'alpha': alpha } if self.__raveled__: if c is not None: self.ax.scatter(self.rfeatures[:, m], self.rfeatures[:, n], c=self.rfeatures[:, c], **defaults) else: self.ax.scatter(self.rfeatures[:, m], self.rfeatures[:, n], **defaults) else: i = j = r # Select square subsets if c is not None: # Plot the data; if a third dimension in color is requested... self.ax.scatter(self.rfeatures[0:i, 0:j, m], self.rfeatures[0:i, 0:j, n], c=self.rfeatures[0:i, 0:j, c], **defaults) else: self.ax.scatter(self.rfeatures[0:i, 0:j, m], self.rfeatures[0:i, 0:j, n], **defaults) if c is not None: plt.colorbar(orientation='vertical') t = '2D Projection with Axis %d in Color' % (c + 1) else: t = '2D Projection' # Allow users to change the x- and y-axis limits axes = plt.gca() if xlim is not None: axes.set_xlim(xlim) if ylim is not None: axes.set_ylim(ylim) plt.xlabel(xtpl % (m + 1), fontsize=14) plt.ylabel(ytpl % (n + 1), fontsize=14) plt.suptitle(stitle % (m + 1, n + 1), fontsize=18, fontweight='bold') plt.title(t, fontsize=16) if not hold: plt.show() if not hold and interact: self.on_reset() self.ax.figure.canvas.mpl_connect('button_press_event', self.on_press) return self.ax
plt.rcParams['legend.labelspacing']=0.1 figsize=(7.5,3) sub = df.query('n_ens==10') plt.figure(figsize=(20,11)) plt.subplot(3, 2, 1) # convert rmse to m2/a2 plt.plot(sub['leadtime'], sub['rmse_ensmean']*9.81, label='ensmean', color='black') plt.plot(sub['leadtime'], sub['rmse_ctrl']*9.81, label='ctrl') plt.plot(sub['leadtime'], sub['spread']*9.81, label='spread',color='black', linestyle='--') plt.legend(loc='upper left', fontsize=14) plt.ylabel('rmse (solid) \n spread (dashed) [$m^2/s^2$]') plt.xlabel('leadtime [h]') plt.xlabel('leadtime [h]') sns.despine() plt.suptitle(f'GEFS reforecasts') plt.subplot(3, 2, 5) plt.plot(sub['leadtime'], sub['crps']*9.81, color='black') plt.ylabel('crps [$m^2/s^2$]') plt.xlabel('leadtime [h]') plt.xlabel('leadtime [h]') sns.despine() plt.subplot(3, 2, 3) plt.plot(sub['leadtime'][1:], sub['corr'][1:], label='svd', color='black') plt.xlabel('leadtime [h]') plt.ylabel('spread-error correlation') sns.despine() plt.ylim((0, 0.9))
for i in range(0, len(init_dates), 20): init_date = init_dates[i] gefs = data.sel(time=init_date) svd = data_svd[i] rand = data_rand[i] netens = data_netens[i] svd = xr.DataArray(data=svd, coords=gefs.coords, dims=gefs.dims) rand = xr.DataArray(data=rand, coords=gefs.coords, dims=gefs.dims) netens = xr.DataArray(data=netens, coords=gefs.coords, dims=gefs.dims) plt.figure() plt.subplot(221) gefs.plot() plt.title('gefs') plt.subplot(222) svd.plot() plt.title('svd') plt.subplot(223) rand.plot() plt.title('rand') plt.subplot(224) netens.plot() plt.title('multitrain') plt.suptitle(init_date.strftime('%Y%m%d')) plt.tight_layout() plt.savefig(f'plots/ensmean_snapshots_{i:03d}.png', dpi=300) plt.savefig(f'plots/ensmean_snapshots_{i:03d}.pdf')
df = pd.concat(res_df) df.to_csv(f'{plotdir}/gen_and_real_ameans_{params}_{epoch:04d}.csv') # make boxplot for showfliers in (True, False): plt.figure() plt.subplot(211) sns.boxplot('hour', 'precip', data=df, hue='typ', showfliers=showfliers) plt.xlabel('') sns.despine() plt.subplot(212) sns.boxplot('hour', 'fraction', data=df, hue='typ', showfliers=showfliers) sns.despine() plt.suptitle(f'n={n_sample}') plt.savefig( f'{plotdir}/daily_cycle_showfliers{showfliers}_{params}_{epoch:04d}.svg' ) ## for a single real one, generate a large # number of fake distributions, and then # plot the areamean in a lineplot # we generate 100 fake distributions with different noise accross the samples # and additionally 10 fake ones that use the same noise for all plots # the latter we plot in the same color (1 seperate color for each generated one) # so that we can compare them accross the plots n_to_generate = 20 n_fake_per_real = 100 n_fake_per_real_samenoise = 10
plt.figure() for i,xval in enumerate(x[0]): if i==0: rxmin, rxmax = xval-0.004, xval+0.004 else: rxmin, rxmax = xval-0.03, xval+0.03 rymin, rymax = np.min(y[:,i]), np.max(y[:,i]) rect = plt.Rectangle((rxmin, rymin), rxmax-rxmin, rymax-rymin, facecolor='grey',alpha=0.4) plt.gca().add_patch(rect) for i,sc in enumerate(loaded): plt.plot(x[i],y[i]) if nc != 8: plt.semilogy() #if nc in yldict: plt.ylim(yldict[nc]) plt.xlabel('FES / maxFES') plt.ylabel(r'error = $f_i(x)-f_i(x^*)$') plt.suptitle(ttxt,x=0.5,y=0.98, ha='center',va='top', fontsize=10) plt.suptitle(date,x=0.97,y=0.02, ha='right',va='bottom', fontsize=8) plt.savefig(join(plotloc,'allruns_c'+str(nc).zfill(3)+'_'+eat+'_'+df[:-1]+'.png')) plt.close() # for i,xval in enumerate(x[0]): # if i==0: # rxmin, rxmax = xval-0.004, xval+0.004 # else: # rxmin, rxmax = xval-0.03, xval+0.03 # rymin, rymax = np.min(y[:,i]), np.max(y[:,i]) # rect = plt.Rectangle((rxmin, rymin), rxmax-rxmin, rymax-rymin, facecolor='grey',alpha=0.4) # plt.gca().add_patch(rect) # plt.plot(x[0,:],np.min(y,axis=0),c='c') # plt.plot(x[0,:],np.max(y,axis=0),c='c') # if nc != 8: plt.semilogy()
else: rxmin, rxmax = xval - 0.03, xval + 0.03 rymin, rymax = np.min(y[:, i]), np.max(y[:, i]) rect = plt.Rectangle((rxmin, rymin), rxmax - rxmin, rymax - rymin, facecolor='grey', alpha=0.4) plt.gca().add_patch(rect) for i, sc in enumerate(loaded): plt.plot(x[i], y[i]) if nc != 8: plt.semilogy() #if nc in yldict: plt.ylim(yldict[nc]) plt.xlabel('FES / maxFES') plt.ylabel(r'error = $f_i(x)-f_i(x^*)$') plt.suptitle(ttxt, x=0.5, y=0.98, ha='center', va='top', fontsize=10) plt.suptitle(date, x=0.97, y=0.02, ha='right', va='bottom', fontsize=8) plt.savefig( join( plotloc, 'allruns_c' + str(nc).zfill(3) + '_' + eat + '_' + df[:-1] + '.png')) plt.close() # for i,xval in enumerate(x[0]): # if i==0: # rxmin, rxmax = xval-0.004, xval+0.004 # else: # rxmin, rxmax = xval-0.03, xval+0.03 # rymin, rymax = np.min(y[:,i]), np.max(y[:,i]) # rect = plt.Rectangle((rxmin, rymin), rxmax-rxmin, rymax-rymin, facecolor='grey',alpha=0.4) # plt.gca().add_patch(rect)
for i in range(h): yc = ylim[1] - i * dy for j in range(m): xc = xlim[0] + j * dx dat[j * n] = xc dat[j * n + 1] = yc #print "first DNA: ",dat[0],dat[1]; #print "2nd DNA: ",dat[2],dat[3]; #print "last DNA: ",dat[2*m-2],dat[2*m-1]; r1 = tf.test_func(dat, f, ct.c_int(n), ct.c_int(m), ct.c_int(k)) rarr[i, :] = [f[j] for j in range(m)] plt.pcolor(x, y, flipud(rarr), vmin=np.min(rarr), vmax=np.max(rarr)) plt.colorbar() plt.xlim(xlim) plt.ylim(ylim) plt.xlabel(r'$x_1$') plt.ylabel(r'$x_2$') plt.title('CEC-2013 test function suite:\nno. {0}: {1}'.format(k, fnames[k])) plt.suptitle('evaluated using ctypes', x=0.02, y=0.02, ha='left', va='bottom', fontsize=9) #plt.show() plt.savefig('./pics/test_func_' + str(k).zfill(2) + '_using_ctypes_zoomlevel_2.png') plt.clf() plt.close('all')
# initially the k-loop was here, and it didn't work for any plot except the first one # for k in range(1,29): # so, this didn't work print 'now function ',k for i in range(h): yc=ylim[1]-i*dy for j in range(m): xc=xlim[0]+j*dx; dat[j*n]=xc; dat[j*n+1]=yc; #print "first DNA: ",dat[0],dat[1]; #print "2nd DNA: ",dat[2],dat[3]; #print "last DNA: ",dat[2*m-2],dat[2*m-1]; r1=tf.test_func(dat,f,ct.c_int(n),ct.c_int(m),ct.c_int(k)) rarr[i,:]=[f[j] for j in range(m)] plt.pcolor(x,y,flipud(rarr),vmin=np.min(rarr),vmax=np.max(rarr)) plt.colorbar() plt.xlim(xlim) plt.ylim(ylim) plt.xlabel(r'$x_1$') plt.ylabel(r'$x_2$') plt.title('CEC-2013 test function suite:\nno. {0}: {1}'.format(k,fnames[k])) plt.suptitle('evaluated using ctypes',x=0.02,y=0.02,ha='left',va='bottom',fontsize=9) #plt.show() plt.savefig('./pics/test_func_'+str(k).zfill(2)+'_using_ctypes_zoomlevel_2.png') plt.clf() plt.close('all')