Optimizer not using declared partials? - openmdao

I am using OpenMDAO to drive a CFD optimization. The computational geometry, meshing, and solver execution is handled by a separate python script which takes in the updated design. In one execution, the script will solve for the forces, but also the derivatives through an adjoint analysis. My issue is that OpenMDAO only seems to be using the derivatives, which I have specified, part of the time and not for every iteration. Below is a sample of the code that I am using:
import os
import argparse
import numpy as np
from matplotlib import pyplot as plt
import csv
import f90nml
import socket
import sys
import pickle
# Import OpenMDAO module - currently tested against version 2.8.0
from openmdao.api import Problem, Group, IndepVarComp, ExplicitComponent, ScipyOptimizeDriver
import openmdao.api as om
class FUN3DExternalCodeComp(om.ExternalCodeComp):
def setup(self):
# Design Variables
self.add_input('alpha', val=0.0)
self.add_input('TE1', val=0.0)
self.add_input('TE2', val=0.0)
self.add_input('TE3', val=0.0)
self.add_input('TE4', val=0.0)
self.add_input('TE5', val=0.0)
self.add_input('TE6', val=0.0)
self.add_output('CD', val=0.0)
self.add_output('CL', val=0.0)
self.add_output('CY', val=0.0)
self.add_output('CMx', val=0.0)
self.add_output('CMy', val=0.0)
self.add_output('CMz', val=0.0)
self.input_file = 'designVar.in'
self.output_file = 'fun3dResponse.out'
self.derivs_file = 'fun3dDerivs.out'
# providing these is optional; the component will verify that any input
# files exist before execution and that the output files exist after.
self.options['external_input_files'] = [self.input_file]
self.options['external_output_files'] = [self.output_file, self.derivs_file]
self.options['command'] = [
# sys.executable, 'runFun3DsensPA_Euler.py',
sys.executable, 'function.py',
self.input_file, self.output_file, self.derivs_file]
# this external code does provide derivatives
self.declare_partials(of='*', wrt='*')
def compute(self, inputs, outputs):
alpha = inputs['alpha']
TE1 = inputs['TE1']
TE2 = inputs['TE2']
TE3 = inputs['TE3']
TE4 = inputs['TE4']
TE5 = inputs['TE5']
TE6 = inputs['TE6']
# generate the input file for the paraboloid external code
with open(self.input_file, 'w') as input_file:
input_file.write('%.16f\n%.16f\n%.16f\n%.16f\n%.16f\n%.16f\n%.16f\n' % (alpha, TE1, TE2, TE3, TE4, TE5, TE6))
# the parent compute function actually runs the external code
super(FUN3DExternalCodeComp, self).compute(inputs, outputs)
# parse the output file from the external code and set the value of f_xy
with open(os.path.join(self.output_file), 'r') as output_file:
outputs['CL'] = float(output_file.readline())
outputs['CD'] = float(output_file.readline())
outputs['CY'] = float(output_file.readline())
outputs['CMx'] = float(output_file.readline())
outputs['CMy'] = float(output_file.readline())
outputs['CMz'] = float(output_file.readline())
print('OUTPUTS', outputs)
def compute_partials(self, inputs, partials):
# parse the derivs file from the external code and set partials
with open(os.path.join(self.derivs_file), 'r') as derivs_file:
partials['CL', 'alpha'] = float(derivs_file.readline())
partials['CL', 'TE1'] = float(derivs_file.readline())
partials['CL', 'TE2'] = float(derivs_file.readline())
partials['CL', 'TE3'] = float(derivs_file.readline())
partials['CL', 'TE4'] = float(derivs_file.readline())
partials['CL', 'TE5'] = float(derivs_file.readline())
partials['CL', 'TE6'] = float(derivs_file.readline())
partials['CD', 'alpha'] = float(derivs_file.readline())
partials['CD', 'TE1'] = float(derivs_file.readline())
partials['CD', 'TE2'] = float(derivs_file.readline())
partials['CD', 'TE3'] = float(derivs_file.readline())
partials['CD', 'TE4'] = float(derivs_file.readline())
partials['CD', 'TE5'] = float(derivs_file.readline())
partials['CD', 'TE6'] = float(derivs_file.readline())
partials['CY', 'alpha'] = float(derivs_file.readline())
partials['CY', 'TE1'] = float(derivs_file.readline())
partials['CY', 'TE2'] = float(derivs_file.readline())
partials['CY', 'TE3'] = float(derivs_file.readline())
partials['CY', 'TE4'] = float(derivs_file.readline())
partials['CY', 'TE5'] = float(derivs_file.readline())
partials['CY', 'TE6'] = float(derivs_file.readline())
# Rolling moment partials
partials['CMx', 'alpha'] = float(derivs_file.readline())
partials['CMx', 'TE1'] = float(derivs_file.readline())
partials['CMx', 'TE2'] = float(derivs_file.readline())
partials['CMx', 'TE3'] = float(derivs_file.readline())
partials['CMx', 'TE4'] = float(derivs_file.readline())
partials['CMx', 'TE5'] = float(derivs_file.readline())
partials['CMx', 'TE6'] = float(derivs_file.readline())
# Pitching moment partials
partials['CMy', 'alpha'] = float(derivs_file.readline())
partials['CMy', 'TE1'] = float(derivs_file.readline())
partials['CMy', 'TE2'] = float(derivs_file.readline())
partials['CMy', 'TE3'] = float(derivs_file.readline())
partials['CMy', 'TE4'] = float(derivs_file.readline())
partials['CMy', 'TE5'] = float(derivs_file.readline())
partials['CMy', 'TE6'] = float(derivs_file.readline())
# Yaw moment partials
partials['CMz', 'alpha'] = float(derivs_file.readline())
partials['CMz', 'TE1'] = float(derivs_file.readline())
partials['CMz', 'TE2'] = float(derivs_file.readline())
partials['CMz', 'TE3'] = float(derivs_file.readline())
partials['CMz', 'TE4'] = float(derivs_file.readline())
partials['CMz', 'TE5'] = float(derivs_file.readline())
partials['CMz', 'TE6'] = float(derivs_file.readline())
print('PARTIALS', partials)
# Initialize OpenMDAO Problem
prob = om.Problem()
model = prob.model
# create and connect inputs
model.add_subsystem('p', FUN3DExternalCodeComp())
# Add Design variables
prob.model.add_design_var('p.alpha', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE1', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE2', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE3', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE4', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE5', lower = 1.0, upper = 10)
prob.model.add_design_var('p.TE6', lower = 1.0, upper = 10)
# Lift constraint
prob.model.add_constraint('p.CL', lower = 10, upper = 120)
prob.model.add_constraint('p.CY', lower = 20, upper = 250)
prob.model.add_constraint('p.CMx', lower = 20, upper = 250)
prob.model.add_constraint('p.CMy', lower = 20, upper = 250)
prob.model.add_constraint('p.CMz', lower = 20, upper = 250)
# Minimize Drag
prob.model.add_objective('p.CD')
prob.driver = om.pyOptSparseDriver(optimizer='SLSQP')
prob.driver.options['print_results'] = True
prob.driver.options['debug_print'] = ['desvars','ln_cons','nl_cons','objs','totals']
# prob.driver = om.ScipyOptimizeDriver()
# prob.driver.options['optimizer'] = 'SLSQP'
# prob.driver.options['tol'] = 1e-5
# prob.driver.options['disp'] = True
# prob.driver.options['maxiter'] = 20 # Maximum number of solver iterations
# prob.driver.options['debug_print'] = ['desvars','ln_cons','nl_cons','objs','totals']
prob.driver.recording_options['record_objectives'] = True
prob.driver.recording_options['record_constraints'] = True
prob.driver.recording_options['record_desvars'] = True
prob.driver.recording_options['record_derivatives'] = True
recorder = om.SqliteRecorder('results.sql')
prob.driver.add_recorder(recorder)
prob.set_solver_print(level=0)
prob.setup()
# Set input values
prob.set_val('p.alpha', 3.0)
prob.set_val('p.TE1', 1.0)
prob.set_val('p.TE2', 2.0)
prob.set_val('p.TE3', 3.0)
prob.set_val('p.TE4', 4.0)
prob.set_val('p.TE5', 5.0)
prob.set_val('p.TE6', 6.0)
prob.run_driver()
Exception: '_auto_ivc.v0'
====================================================================== Traceback (most recent call last):
File "/Users/deslicjp/Documents/Software/OpenMDAO/openmdao/drivers/pyoptsparse_driver.py", line 593, in _gradfunc
sens_dict = self._compute_totals(of=self._quantities,
File "/Users/deslicjp/Documents/Software/OpenMDAO/openmdao/core/driver.py", line 849, in _compute_totals
totals = total_jac.compute_totals()
File "/Users/deslicjp/Documents/Software/OpenMDAO/openmdao/core/total_jac.py", line 1414, in compute_totals
self._print_derivatives()
File "/Users/deslicjp/Documents/Software/OpenMDAO/openmdao/core/total_jac.py", line 1629, in _print_derivatives
pprint.pprint({(of, wrt): J[of][wrt]})
KeyError: '_auto_ivc.v0'
======================================================================
capi_return is NULL
Call-back cb_slgrad_in_slsqp__user__routines failed.
Traceback (most recent call last):
UPDATE: 08-07-2020 15:32
Any insight is appreciated!
Thanks,
Josh

Related

How to plot learning curve for my gan in python

I want to plot the learning curve (loss & accuracy plot) for my gan? I try different solution but still can't plot the learning curve for my gan. I tried to use fit and history function but I get error and can't plot loss & accuracy curve. If anyone can help?
I run my code in google colab. Since I only train the discriminator I tried to use fit & history function in the definition of discriminator & gan but still get error
from tensorflow.keras.layers import Dense, Conv2DTranspose, LeakyReLU, Reshape, BatchNormalization, Activation, Conv2D
from tensorflow.keras.models import Model, Sequential
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.layers import Conv2D, Flatten, Dropout
from tensorflow.keras import optimizers
import tensorflow as tf
def image_generator():
generator = Sequential()
generator.add(Dense(256*4*4, input_shape = (100,)))
#generator.add(BatchNormalization())
generator.add(LeakyReLU())
generator.add(Reshape((4,4,256)))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2DTranspose(128,kernel_size=3, strides=2, padding = "same"))
#generator.add(BatchNormalization())
generator.add(LeakyReLU(alpha=0.2))
generator.add(Conv2D(3,kernel_size=3, padding = "same", activation='tanh'))
return(generator)
model_generator = image_generator()
model_generator.summary()
# Define input data
def generate_data_entry(n_samples):
X = np.random.randn(100 * n_samples)
X = X.reshape(n_samples, 100)
return X
def create_data_fake(model_generator, n_samples):
input = generate_data_entry(n_samples)
X = model_generator.predict(input)
y = np.zeros((n_samples, 1))
return X,y
number_samples = 4
X,_ = create_data_fake(model_generator, number_samples)
# Visualize results
for i in range(number_samples):
plt.subplot(2, 2, 1 + i)
plt.axis('off')
plt.imshow(X[i])
def images_discriminator():
discriminator = Sequential()
discriminator.add(Conv2D(64, kernel_size=3, padding = "same", input_shape = (32,32,3)))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(128, kernel_size=3,strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(128, kernel_size=3,strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Conv2D(256, kernel_size=3, strides=(2,2), padding = "same"))
discriminator.add(LeakyReLU(alpha=0.2))
#discriminator.add(Dropout(0.2))
discriminator.add(Flatten())
discriminator.add(Dropout(0.4))
discriminator.add(Dense(1, activation='sigmoid'))
opt = tf.keras.optimizers.Adam(learning_rate=0.0002 ,beta_1=0.5)
discriminator.compile(loss='binary_crossentropy', optimizer= opt , metrics = ['accuracy'])
return(discriminator)
model_discriminator = images_discriminator()
model_discriminator.summary()
from tensorflow.keras.datasets import cifar10
import random
import tensorflow as tf
import numpy as np
from tensorflow.keras import backend
import pandas as pd
import matplotlib.pyplot as plt
import keras
%matplotlib inline
(Xtrain, Ytrain), (testx, testy) = cifar10.load_data()
def load_images():
(Xtrain, Ytrain), (testx, testy) = cifar10.load_data()
# we stayed with the dogs
indice = np.where(Ytrain == 0)
indice = indice[0]
Xtrain = Xtrain[indice, :,:,:]
# Normalize the data
X = Xtrain.astype('float32')
X = (X - 127.5) / 127.5
return X
print(load_images().shape)
def load_data_reales(dataset, n_samples):
ix = np.random.randint(0, dataset.shape[0], n_samples)
X = dataset[ix]
y = np.ones((n_samples, 1))
return X,y
def load_data_fake(n_samples):
X = np.random.rand(32 * 32 * 3 * n_samples)
X = -1 + X * 2
X = X.reshape((n_samples, 32,32,3))
y = np.zeros((n_samples, 1))
return X,y
def train_discriminator(model, dataset, n_iterations=20, batch = 128):
medium_batch = int(batch/2)
for i in range(n_iterations):
X_real, y_real = load_data_reales(dataset, medium_batch)
los_real, acc_real = model.train_on_batch(X_real, y_real)
X_fake, y_fake = load_data_fake(medium_batch)
los_fake, acc_fake = model.train_on_batch(X_fake, y_fake)
print(str(i+1) + ' Real:' + str(acc_real*100) + ', Fake:' + str(acc_fake*100))
dataset = load_images()
train_discriminator(model_discriminator, dataset)
def my_loss_function(x_real,x_fake):
la=tf.keras.losses.BinaryCrossentropy()
my_loss_function = la(x_real, x_fake)
return my_loss_function
def create_gan(discriminator, generator):
discriminator.trainable=False
gan = Sequential()
gan.add(generator)
gan.add(discriminator)
opt = tf.keras.optimizers.Adam(learning_rate=0.0002,beta_1=0.5)
return gan
gan = create_gan(model_discriminator,model_generator)
gan.summary()
def show_images_generated(data_fake, epoch):
now = datetime.now()
now = now.strftime("%Y%m%d_%H%M%S")
# we make the data go from 0 to 1
data_fake = (data_fake + 1) / 2.0
for i in range(10):
plt.imshow(data_fake[i])
plt.axis('off')
name = str(epoch) + '_generated_image_' + str(i) + '.png'
plt.savefig(name, bbox_inches='tight')
plt.close()
def evaluate_and_save(model_generator, epoch, medium_dataset):
# We save the model
now = datetime.now()
now = now.strftime("%Y%m%d_%H%M%S")
name = str(epoch) + '_' + str(now)+"_model_generator_" + '.h5'
model_generator.save(name)
# We generate new data
X_real,Y_real = load_data_reales(dataset, medium_dataset)
X_fake, Y_fake = create_data_fake(model_generator,medium_dataset)
# We evaluate the model
_, acc_real = model_discriminator.evaluate(X_real, Y_real)
_, acc_fake = model_discriminator.evaluate(X_fake, Y_fake)
print('Acc Real:' + str(acc_real*100) + '% Acc Fake:' + str(acc_fake*100)+'%')
def training(data, model_generator, model_discriminator, epochs, n_batch, beginning = 0):
dimension_batch = int(data.shape[0]/n_batch)
medium_dataset = int(n_batch/2)
# We iterate over the epochs
for epoch in range(beginning, beginning + epochs):
# We iterate over all batches
for batch in range(n_batch):
# We load all the real data
X_real,Y_real = load_data_reales(dataset, medium_dataset)
# We train the discriminator withEnrenamos discriminator with data reales
cost_discriminator_real, _ = model_discriminator.train_on_batch(X_real, Y_real)
X_fake, Y_fake = create_data_fake(model_generator,medium_dataset)
cost_discriminator_fake, _ = model_discriminator.train_on_batch(X_fake, Y_fake)
# We generate input images for the GAN
X_gan = generate_data_entry(medium_dataset)
Y_gan = np.ones((medium_dataset, 1))
# We train the GAN with fake data
cost_gan = gan.train_on_batch(X_gan, Y_gan)
# Every 10 epochs we show the results and cost
if (epoch+1) % 10 == 0:
evaluate_and_save(model_generator,epoch = epoch, medium_dataset= medium_dataset)
show_images_generated(X_fake, epoch = epoch)
training(dataset, model_generator, model_discriminator, epochs = 10, n_batch=128, beginning = 0)
X_fake, _ = create_data_fake(n_samples=49, model_generator=model_generator)
X_fake = (X_fake+1)/2
for i in range(49):
plt.subplot(7,7,i+1)
plt.axis('off')
plt.imshow(X_fake[i])
# We generate new data
X_real1,Y_real1 = load_data_reales(dataset, int(15))
X_fake1, Y_fake1 = create_data_fake(model_generator,int(15))
# We evaluate the model
_, acc_real = model_discriminator.evaluate(X_real1, Y_real1)
_, acc_fake = model_discriminator.evaluate(X_fake1, Y_fake1)
print('Acc Real:' + str(acc_real*100) + '% Acc Fake:' + str(acc_fake*100)+'%')

Pytorch geometric label prediction using GNN

I'm currently trying to find a way how to get a single label prediction from my GNN.I'd like to create a list of ground truths compared to how the model predicts the label.
My code is as following, ran in google collab:
Should I create a validation step or is there another way about this? I'm very new to this.
# Load the dataset
dataset = TUDataset(
root=".", name="Mutagenicity",
transform=AddSymbols(),
).shuffle()
from torch_geometric.data import DataLoader
loader = DataLoader(dataset, batch_size=32, shuffle=True)
data_batch = next(iter(loader))
print(data_batch)
print("Number of graphs in the batch:",data_batch.num_graphs)
import torch
from torch_scatter import scatter
from torch_geometric.utils import to_dense_adj
class GraphConvolution(torch.nn.Module):
def __init__(self, num_input_features, num_output_features):
super().__init__()
self.W1 = torch.nn.Parameter(torch.randn(num_input_features, num_output_features)*0.1)
self.W2 = torch.nn.Parameter(torch.randn(num_input_features, num_output_features)*0.1)
def forward(self, x, edge_index):
adj = to_dense_adj(edge_index)
neighbors_aggregation = adj # x
out = x # self.W1 + neighbors_aggregation # self.W2
return out
from torch.nn import Linear, LogSoftmax
import torch.nn.functional as F
from torch_geometric.nn import GraphConv, global_add_pool
class GNN(torch.nn.Module):
def __init__(
self,
num_classes,
hidden_dim,
node_features_dim,
edge_features_dim=None
):
super(GNN, self).__init__()
self.hidden_dim = hidden_dim
self.conv1 = GraphConv(node_features_dim, hidden_dim)
self.conv2 = GraphConv(hidden_dim, hidden_dim)
self.conv3 = GraphConv(hidden_dim, hidden_dim)
self.conv4 = GraphConv(hidden_dim, hidden_dim)
self.conv5 = GraphConv(hidden_dim, hidden_dim)
self.fc1 = Linear(hidden_dim, hidden_dim)
self.fc2 = Linear(hidden_dim, num_classes)
self.readout = LogSoftmax(dim=-1)
def forward(self, x, edge_index, batch):
x = F.relu(self.conv1(x, edge_index))
x = F.relu(self.conv2(x, edge_index))
x = F.relu(self.conv3(x, edge_index))
x = F.relu(self.conv4(x, edge_index))
x = F.relu(self.conv5(x, edge_index))
x = global_add_pool(x, batch)
x = F.relu(self.fc1(x))
x = F.dropout(x, p=0.5, training=self.training)
x = self.fc2(x)
return self.readout(x)
from torch_geometric.loader import DataLoader
from tqdm.auto import tqdm
# If possible, we use a GPU
device = "cuda" if torch.cuda.is_available() else "cpu"
print("Using device:", device)
idx_train_end = int(len(dataset) * .5)
idx_valid_end = int(len(dataset) * .7)
BATCH_SIZE = 128
BATCH_SIZE_TEST = len(dataset) - idx_valid_end
# In the test loader we set the natch size to be equal to the size of the whole test set
loader_train = DataLoader(dataset[:idx_train_end], batch_size=BATCH_SIZE, shuffle=True)
loader_valid = DataLoader(dataset[idx_train_end:idx_valid_end], batch_size=BATCH_SIZE, shuffle=True)
loader_test = DataLoader(dataset[idx_valid_end:], batch_size=BATCH_SIZE_TEST, shuffle=False)
from src import compute_accuracy
def train_model(loader_train, loader_valid, model, optimizer, loss_function, N_EPOCHS):
# Prepare empy lists for logging
train_losses = []
train_accs = []
val_accs = []
for epoch in tqdm(range(N_EPOCHS)):
epoch_loss = 0
for batch in tqdm(loader_train, leave=False):
batch.to(device)
out = model(batch.x, batch.edge_index, batch.batch)
loss = loss_function(out, batch.y.flatten())
optimizer.zero_grad()
loss.backward()
optimizer.step()
acc_train = compute_accuracy(model, loader_train)
acc_valid = compute_accuracy(model, loader_valid)
with torch.no_grad():
train_accs.append(acc_train)
val_accs.append(acc_valid)
train_losses.append(loss)
print(f"Epoch: {epoch}, Loss: {loss}")

deconvolution (convTranspose2D) intialization with bilinear interpolation in pytorch?

I am running PyTorch implementation of this article (https://arxiv.org/pdf/1604.03650.pdf).
In the article, it says to initialize the deconv layers with bilinear interpolation which is not in the code. it is told that if in deconvolution, we are scaling the data by factor S, the initial weights are defined as:
enter image description here
Does anyone know how can I implement it?
This is the neural network model designed based on the article:
In another word, I don't know how to initialization the deconvolution layers (for example deconv1 layer in this code).
import torch
import torch.nn as nn
import numpy as np
import torchvision
import torch.nn.functional as F
cfg = [64, 128, 256, 512, 512]
class Deep3d(nn.Module):
def __init__(self, in_channels=3, out_channels=3, device=torch.device('cpu')):
super(Deep3d, self).__init__()
self.device = device
vgg16 = torchvision.models.vgg16_bn(pretrained=True)
modules = []
layer = []
for l in vgg16.features:
if isinstance(l, nn.MaxPool2d):
layer.append(l)
modules.append(layer)
layer = []
else:
layer.append(l)
scale = 1
deconv = []
layer = []
for m in range(len(modules)):
layer.append(nn.Conv2d(cfg[m], cfg[m], kernel_size=3, stride=1, padding=True))
layer.append(nn.ReLU(inplace=True))
layer.append(nn.Conv2d(cfg[m], cfg[m], kernel_size=3, stride=1, padding=True))
layer.append(nn.ReLU(inplace=True))
if(m==0):
layer.append(nn.ConvTranspose2d(cfg[m], 65, kernel_size=1, stride=1, padding=(0,0)))
else:
scale *=2
layer.append(nn.ConvTranspose2d(cfg[m], 65, kernel_size=scale*2, stride=scale, padding=(scale//2, scale//2)))
deconv.append(layer) # add blocks of layers to deconv part of the network
layer = []
self.module_1 = nn.Sequential(*modules[0])
self.module_2 = nn.Sequential(*modules[1])
self.module_3 = nn.Sequential(*modules[2])
self.module_4 = nn.Sequential(*modules[3])
self.module_5 = nn.Sequential(*modules[4])
self.deconv_1 = nn.Sequential(*deconv[0])
self.deconv_2 = nn.Sequential(*deconv[1])
self.deconv_3 = nn.Sequential(*deconv[2])
self.deconv_4 = nn.Sequential(*deconv[3])
self.deconv_5 = nn.Sequential(*deconv[4])
self.linear_module = nn.Sequential(*[nn.Linear(15360,4096), # hyperparam choice
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(4096,1950)]) # 1950=65(disparity range)*10*3(10*3 is feature map size)
self.deconv_6 = nn.Sequential(*[nn.ConvTranspose2d(65,65,kernel_size=scale*2,stride=scale,padding=(scale//2,scale//2))])
self.upconv_final = nn.Sequential(*[nn.ConvTranspose2d(65,65,kernel_size=(4,4),stride=2,padding=(1,1)),
nn.ReLU(inplace=True),
nn.Conv2d(65,65,kernel_size=(3,3),stride=1,padding=(1,1)),
nn.Softmax(dim=1)])
for block in [self.deconv_1,self.deconv_2,self.deconv_3,self.deconv_4,self.deconv_5,self.deconv_6,self.linear_module,self.upconv_final]:
for m in block:
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
nn.init.constant_(m.bias, 0)
def forward(self, orig_x, x):
x_copy = orig_x
pred = []
out_1 = self.module_1(x)
out_2 = self.module_2(out_1)
out_3 = self.module_3(out_2)
out_4 = self.module_4(out_3)
out_5 = self.module_5(out_4)
# print(out_5.shape)
out_5_flatten = out_5.view(x_copy.shape[0],-1)
out_6 = self.linear_module(out_5_flatten)
p1 = self.deconv_1(out_1)
p2 = self.deconv_2(out_2)
p3 = self.deconv_3(out_3)
p4 = self.deconv_4(out_4)
p5 = self.deconv_5(out_5)
# print(p5.shape)
p6 = self.deconv_6(out_6.view(x_copy.shape[0],65,3,10))
pred.append(p1)
pred.append(p2)
pred.append(p3)
pred.append(p4)
pred.append(p5)
pred.append(p6)
out = torch.zeros(pred[0].shape).to(self.device)
for p in pred:
out = torch.add(out, p)
out = self.upconv_final(out) # to be elt wise multiplied with shifted left views
out = F.interpolate(out,scale_factor=4,mode='bilinear') # upscale to match left input size
new_right_image = torch.zeros(x_copy.size()).to(self.device)
stacked_shifted_view = None
stacked_out = None
for depth_map_idx in range(-33,32):
shifted_input_view = torch.zeros(x_copy.size()).to(self.device)
if depth_map_idx<0:
shifted_input_view[:,:,:,:depth_map_idx] = x_copy[:,:,:,-depth_map_idx:]
elif depth_map_idx==0:
shifted_input_view = x_copy
else:
shifted_input_view[:,:,:,depth_map_idx:] = x_copy[:,:,:,:-depth_map_idx]
if stacked_shifted_view is None:
stacked_shifted_view = shifted_input_view.unsqueeze(1)
else:
stacked_shifted_view = torch.cat((stacked_shifted_view,shifted_input_view.unsqueeze(1)),dim=1)
if stacked_out is None:
stacked_out = out[:,depth_map_idx+33:depth_map_idx+34,:,:].unsqueeze(1)
else:
stacked_out = torch.cat((stacked_out,out[:,depth_map_idx+33:depth_map_idx+34,:,:].unsqueeze(1)),dim=1)
softmaxed_stacked_shifted_view = stacked_shifted_view
mult_soft_shift_out = torch.mul(stacked_out,softmaxed_stacked_shifted_view)
final_rt_image = torch.sum(mult_soft_shift_out,dim=1)
return final_rt_image
# if(__name__=='__main__'):
# vgg16 = torchvision.models.vgg16(pretrained=True)
# model = Deep3d().to(torch.device('cpu'))
# out = model(torch.randn(10,3,384,1280),torch.randn(10,3,96,320))

How to embed plot inside wx.SplitterWindow (right panel)?

I'm trying to embed plot inside right panel of Splitter window, how to add plot inside splitter window. please find here the link for the dataset.
https://www.dropbox.com/s/ncy6dlpm79p578s/Dataset.zip?dl=0.
The file contains rows and columns of wavelength and reflectance.
import wx
from pylab import *
import asciitable
import matplotlib.pyplot as plt
import os
from wxmplot import ImageMatrixFrame
class RandomObj(object):
def __init__(self, name):
self.name = name
class SLI(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, size=(820, 450))
splitter = wx.SplitterWindow(self, style = wx.SP_BORDER)
leftPanel = wx.Panel(splitter, size=(400,100))
rightPanel = wx.Panel(splitter, size=(400,100))
####Tree Widget#####
self.tree = wx.TreeCtrl(leftPanel)
leftSizer = wx.BoxSizer(wx.VERTICAL)
leftSizer.Add(self.tree, 1, wx.EXPAND | wx.ALIGN_CENTER)
leftPanel.SetSizer(leftSizer)
rightSizer = wx.BoxSizer(wx.VERTICAL)
self.display = wx.StaticText(rightPanel, -1, '', (10, 10),
style=wx.ALIGN_CENTRE)
rightSizer.Add(self.display, -1, wx.EXPAND)
rightPanel.SetSizer(rightSizer)
splitter.SplitVertically(leftPanel, rightPanel)
##### Splitter ends ####
root = self.tree.AddRoot('Database')
self.tree.AppendItem(root, 'USGS')
files = []
self.dname = []
self.test = []
for dirname, dirnames, filenames in os.walk('.\USGS'):
for filename in filenames:
files.append(os.path.join(dirname, filename))
self.test.append(filename)
self.tree.AppendItem(self.tree.GetLastChild(root), filename)
self.dname = files[:]
self.tree.AppendItem(root,'ASTER')
for dirname, dirnames, filenames in os.walk('.\ASTER'):
for filename in filenames:
files.append(os.path.join(dirname, filename))
self.test.append(filename)
self.tree.AppendItem(self.tree.GetLastChild(root), filename)
self.dname = files[:]
self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.ASTER, self.tree)
def ASTER(self, event):
self.item = event.GetItem()
value1 = self.tree.GetItemText(self.item)
value2 = 0
value3 = 1
self.item=None
for k in self.test:
if value1 == k:
value2 +=1
break
else:
value2 +=1
for i in self.dname:
if value3 == value2:
array =[]
f=open(i, 'r')
for j in xrange(27):
f.next()
for line in f:
array.append(line)
data = asciitable.read(array)
plot(data.col1, data.col2)
title(value1)
show()
break
else:
value3 +=1
app = wx.App(None)
frame = ImageMatrixFrame()
SLI().Show()
app.MainLoop()
how to insert plot window inside right panel of splitter.
I am not 100% sure I understand your code - there are some formatting and indentation problems for sure. I also am not familiar with asciitable. But, that said, a wxmplot.PlotPanel or ImagePanel can be embedded in a wxPython Frame that uses a Splitter. An example might look like the code below. I tried to make it short, but also tried to make it complete and using plain wxPython. For a more complete application, you'd probably want to put the reading of the datafiles into a separate class, etc. Anyway, this uses your Dataset folder, and should mostly work to show the concepts:
#!/usr/bin/env python
import os
import wx
from wxmplot import PlotPanel
# see https://gist.github.com/newville/e805a6454c4e4c0e010bf0b3cc796d52
from asciifile import read_ascii
LEFTSTYLE = wx.ALIGN_LEFT|wx.GROW|wx.ALL
def pack(window, sizer, expand=1.1):
"simple wxPython pack function"
tsize = window.GetSize()
msize = window.GetMinSize()
window.SetSizer(sizer)
sizer.Fit(window)
nsize = (int(1.1*max(msize[0], tsize[0])),
int(1.1*max(msize[1], tsize[1])))
window.SetSize(nsize)
class SpectraPlotterFrame(wx.Frame):
def __init__(self, data_folder):
wx.Frame.__init__(self, None, size=(800, 450))
self.SetTitle("Data File Plotter: {:s}".format(data_folder))
self.data_folder = data_folder
self.current_filename = None
splitter = wx.SplitterWindow(self, style=wx.SP_LIVE_UPDATE)
splitter.SetMinimumPaneSize(200)
# left side: ListBox of File Names
l_panel = wx.Panel(splitter)
l_sizer = wx.BoxSizer(wx.VERTICAL)
self.filelist = wx.ListBox(l_panel)
self.filelist.Bind(wx.EVT_LISTBOX, self.onFileChoice)
l_sizer.Add(self.filelist, 1, LEFTSTYLE, 5)
pack(l_panel, l_sizer)
# right side: Panel to choose plot array labels, make plot
r_panel = wx.Panel(splitter)
r_sizer = wx.GridBagSizer(3, 3)
self.xarr = wx.Choice(r_panel, choices=[], size=(175, -1))
self.yarr = wx.Choice(r_panel, choices=[], size=(175, -1))
xlabel = wx.StaticText(r_panel, label='X:', style=LEFTSTYLE)
ylabel = wx.StaticText(r_panel, label='Y:', style=LEFTSTYLE)
plot_btn = wx.Button(r_panel, label='Show Plot', size=(125, -1))
plot_btn.Bind(wx.EVT_BUTTON, self.onPlot)
self.plotpanel = PlotPanel(r_panel, size=(650, 450))
r_sizer.Add(xlabel, (0, 0), (1, 1), LEFTSTYLE, 2)
r_sizer.Add(self.xarr, (0, 1), (1, 1), LEFTSTYLE, 2)
r_sizer.Add(ylabel, (0, 2), (1, 1), LEFTSTYLE, 2)
r_sizer.Add(self.yarr, (0, 3), (1, 1), LEFTSTYLE, 2)
r_sizer.Add(plot_btn, (0, 4), (1, 1), LEFTSTYLE, 2)
r_sizer.Add(self.plotpanel, (1, 0), (1, 6), LEFTSTYLE, 2)
pack(r_panel, r_sizer)
splitter.SplitVertically(l_panel, r_panel, 1)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(splitter, 1, LEFTSTYLE, 5)
pack(self, sizer)
wx.CallAfter(self.read_datafiles)
self.Show()
self.Raise()
def read_datafiles(self):
self.datasets = {}
dfolder = os.path.abspath(self.data_folder)
for fname in sorted(os.listdir(self.data_folder)):
try:
self.datasets[fname] = read_ascii(os.path.join(dfolder, fname))
except:
print("Could not read file {:s}".format(fname))
self.filelist.Append(fname)
def onFileChoice(self, event=None):
self.current_filename = fname = event.GetString()
for choice, default in ((self.xarr, 0), (self.yarr, 1)):
choice.Clear()
choice.AppendItems(self.datasets[fname].array_labels)
choice.SetSelection(default)
def onPlot(self, event=None):
x = self.xarr.GetSelection()
y = self.yarr.GetSelection()
xlab = self.xarr.GetStringSelection()
ylab = self.yarr.GetStringSelection()
if self.current_filename is not None:
dset = self.datasets[self.current_filename]
self.plotpanel.plot(dset.data[x], dset.data[y], xlabel=xlab,
ylabel=ylab, label=self.current_filename,
show_legend=True)
class SpectraPlotterApp(wx.App):
def __init__(self, data_folder='.', **kws):
self.data_folder = data_folder
wx.App.__init__(self, **kws)
def createApp(self):
frame = SpectraPlotterFrame(data_folder=self.data_folder)
self.SetTopWindow(frame)
def OnInit(self):
self.createApp()
return True
if __name__ == '__main__':
SpectraPlotterApp(data_folder='Dataset').MainLoop()

"ValueError: year is out of range" when attempting to use matplotlib pyplot

I am attempting to get a matplotlib plotting function to be able to produce a graph with the x-axis set as a time axis. However, when I attempt to plot some values against UNIX times, I encounter the error ValueError: year is out of range. What is going wrong and how can it be addressed?
import os
import time
import matplotlib.dates
import matplotlib.pyplot
import shijian
def main():
data = [
[1484611200.0, 844.4333],
[1484524800.0, 783.3373],
[1484438400.0, 774.194 ],
[1484352000.0, 769.2299]
]
save_graph_matplotlib(
values = data,
line = True,
line_width = 0.5,
title_axis_x = "time",
title_axis_y = "value",
#time_axis_x = True
)
def save_graph_matplotlib(
values = None,
title = None,
title_axis_x = None,
title_axis_y = None,
filename = None,
directory = ".",
overwrite = True,
color = "black",
LaTeX = False,
markers = True,
marker_size = 1,
aspect = None,
line = False,
line_style = "-",
line_width = 0.2,
font_size = 20,
scientific_notation = False,
time_axis_x = False
):
# 1D or 2D data
if isinstance(values[0], list):
x = [element[0] for element in values]
y = [element[1] for element in values]
else:
x = range(0, len(values))
y = values
matplotlib.pyplot.ioff()
if LaTeX is True:
matplotlib.pyplot.rc("text", usetex = True)
matplotlib.pyplot.rc("font", family = "serif")
if filename is None:
if title is None:
filename = "graph.png"
else:
filename = shijian.propose_filename(
filename = title + ".png",
overwrite = overwrite
)
else:
filename = shijian.propose_filename(
filename = filename,
overwrite = overwrite
)
figure = matplotlib.pyplot.figure()
if title is not None:
figure.suptitle(
title,
fontsize = font_size
)
if markers is True:
matplotlib.pyplot.scatter(
x,
y,
s = marker_size,
c = color,
edgecolors = "none",
)
if line is True:
matplotlib.pyplot.plot(
x,
y,
line_style,
c = color,
linewidth = line_width
)
# Turn on or off axes scientific notation.
if scientific_notation is False:
matplotlib.pyplot.gca().get_xaxis().\
get_major_formatter().set_scientific(False)
matplotlib.pyplot.gca().get_yaxis().\
get_major_formatter().set_scientific(False)
# Set axes titles.
if title_axis_x is not None:
matplotlib.pyplot.xlabel(title_axis_x, fontsize = font_size)
if title_axis_y is not None:
matplotlib.pyplot.ylabel(title_axis_y, fontsize = font_size)
# Set axes font size.
matplotlib.pyplot.xticks(fontsize = font_size)
matplotlib.pyplot.yticks(fontsize = font_size)
# Set or do not set axis x as time.
if time_axis_x:
time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
matplotlib.pyplot.axes().xaxis_date()
matplotlib.pyplot.axes().xaxis.set_major_formatter(time_formatter)
matplotlib.pyplot.xticks(rotation = -90)
# Set the aspect ratio.
if aspect is None:
matplotlib.pyplot.axes().set_aspect(
1 / matplotlib.pyplot.axes().get_data_ratio()
)
else:
matplotlib.pyplot.axes().set_aspect(aspect)
if not os.path.exists(directory):
os.makedirs(directory)
matplotlib.pyplot.savefig(
directory + "/" + filename,
dpi = 700
)
matplotlib.pyplot.close()
if __name__ == "__main__":
main()
You need to convert your timestamp-like x data to a python datetime object, which can then be used in matplotlib and be understood by the matplotlib.dates.DateFormatter.
This can be done using the datetime.datetime.fromtimestamp() method.
import datetime
import matplotlib.dates
import matplotlib.pyplot as plt
data = [
[1484611200.0, 844.4333],
[1484524800.0, 783.3373],
[1484438400.0, 774.194 ],
[1484352000.0, 769.2299]
]
x = [datetime.datetime.fromtimestamp(element[0]) for element in data]
y = [element[1] for element in data]
plt.plot( x, y, ls="-", c= "b", linewidth = 2 )
plt.xlabel("Dates")
time_formatter = matplotlib.dates.DateFormatter("%Y-%m-%d")
plt.axes().xaxis.set_major_formatter(time_formatter)
plt.axes().xaxis_date() # this is not actually necessary
plt.show()
Whilst not directly addressing the text of the question, the error mentioned in the title can also occur when one attempts to plot data on an existing axis whose timeline units don't match those of the plot data (e.g. seconds vs datetime).

Resources