I am working on MERRA data in Matlab. I am interested in plotting the data for specified co-ordinates for MERRA300.prod.assim.tavg1_2d_rad_Nx product.
I need your help regarding how to extract specific location data (based on latitude/Longitude)?
I am attaching a code that I have worked on.
I have also seen examples on here and here.
file_name = 'MERRA300.prod.assim.tavg1_2d_rad_Nx.20150101.hdf';
GRID_NAME = 'EOSGRID'
file_id = hdfgd('open', file_name, 'rdonly');
grid_name='EOSGRID';
grid_id = hdfgd('attach', file_id, grid_name);
% datafield_name='U500';
datafield_name='ALBEDO';
[data, fail] = hdfgd('readfield', grid_id, datafield_name, [], [], []);
data=squeeze(double(data(:,:,1)));
data=data';
% size(data)
datafield_name='XDim';
[lon, status] = hdfgd('readfield', grid_id, datafield_name, [], [], []);
lon=double(lon);
datafield_name='YDim';
[lat, status] = hdfgd('readfield', grid_id, datafield_name, [], [], []);
lat=double(lat);
hdfgd('detach', grid_id);
hdfgd('close', file_id);
SD_id = hdfsd('start',file_name, 'rdonly');
% datafield_name='U500';
datafield_name='ALBEDO';
% size(lat)
% size(lon)
sds_index = hdfsd('nametoindex', SD_id, datafield_name);
sds_id = hdfsd('select',SD_id, sds_index);
fillvalue_index = hdfsd('findattr', sds_id, '_FillValue');
[fillvalue, status] = hdfsd('readattr',sds_id, fillvalue_index);
missingvalue_index = hdfsd('findattr', sds_id, 'missing_value');
[missingvalue, status] = hdfsd('readattr',sds_id, missingvalue_index);
long_name_index = hdfsd('findattr', sds_id, 'long_name');
[long_name, status] = hdfsd('readattr',sds_id, long_name_index);
units_index = hdfsd('findattr', sds_id, 'units');
[units, status] = hdfsd('readattr',sds_id, units_index);
scale_index = hdfsd('findattr', sds_id, 'scale_factor');
[scale, status] = hdfsd('readattr',sds_id, scale_index);
scale = double(scale);
offset_index = hdfsd('findattr', sds_id, 'add_offset');
[offset, status] = hdfsd('readattr',sds_id, offset_index);
offset = double(offset);
hdfsd('endaccess', sds_id);
data(data==fillvalue) = NaN;
data(data==missingvalue) = NaN;
data = data*scale + offset ;
latlim = [floor(min(min(lat))),ceil(max(max(lat)))];
lonlim = [floor(min(min(lon))),ceil(max(max(lon)))];
min_data = floor(min(min(data)));
max_data = ceil(max(max(data)));
% Find indexes for region along longitude.
lon_region = (lon > 18.0 & lon < 45.0);
i = find(lon_region,1,'first');
j = find(lon_region,1,'last');
% Subset data using the above indices.
% lat = lat(i:j);
% lon = lon(i:j);
% time = time(i:j);
% data = data(:,i:j);
% Find indexes region along latitude.
lat_region = (lat > 18.0 & lat < 45.0);
i = find(lat_region,1,'first');
j = find(lat_region,1,'last');
% Subset data using the above indices.
lat = lat(i:j);
lon = lon(i:j);
% time = time(i:j);
% data = data(:,i:j);
f = figure('Name', file_name,'visible','on');
axesm('MapProjection','eqdcylin','Frame','on','Grid','on', ...
'MapLatLimit',latlim,'MapLonLimit',lonlim, ...
'MeridianLabel','on','ParallelLabel','on')
coast = load('coast.mat');
% surfacem(lat,lon,data);
surfm(lat,lon,data);
colormap('Jet');
caxis([min_data max_data]);
% Change the value if you want to have more than 10 tick marks.
ntickmarks = 10;
granule = floor((max_data - min_data) / ntickmarks);
h=colorbar('YTick', min_data:granule:max_data);
plotm(coast.lat,coast.long,'k')
title({file_name; ...
[long_name [' at TIME=0']]}, ...
'Interpreter', 'None', 'FontSize', 16,'FontWeight','bold');
set (get(h, 'title'), 'string', units, 'FontSize', 16,'FontWeight','bold');
% Use the following if your screen isn't too big (> 1024 x 768).
scrsz = get(0,'ScreenSize');
% The following fixed-size screen size will look better in PNG if
% your screen is too large.
scrsz = [1 1 800 600];
set(f,'position',scrsz,'PaperPositionMode','auto');
Related
I've been trying figure out what I have done wrong for many hours, but just can't figure out. I've even looked at other basic Neural Network libraries to make sure that my gradient descent algorithms were correct, but it still isn't working.
I'm trying to teach it XOR but it outputs -
input (0 0) | 0.011441891321516094
input (1 0) | 0.6558508610135193
input (0 1) | 0.6558003273099053
input (1 1) | 0.6563021185296245
after 1000 trainings, so clearly there's something wrong.
The code is written in lua and I created the Neural Network from raw data so you can easily understand how the data is formatted.
- Training code -
math.randomseed(os.time())
local nn = require("NeuralNetwork")
local network = nn.newFromRawData({
["activationFunction"] = "sigmoid",
["learningRate"] = 0.3,
["net"] = {
[1] = {
[1] = {
["value"] = 0
},
[2] = {
["value"] = 0
}
},
[2] = {
[1] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[2] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[3] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
},
[4] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1
}
}
},
[3] = {
[1] = {
["bias"] = 1,
["netInput"] = 0,
["value"] = 0,
["weights"] = {
[1] = 1,
[2] = 1,
[3] = 1,
[4] = 1
}
}
}
}
})
attempts = 1000
for i = 1,attempts do
network:backPropagate({0,0},{0})
network:backPropagate({1,0},{1})
network:backPropagate({0,1},{1})
network:backPropagate({1,1},{0})
end
print("Results:")
print("input (0 0) | "..network:feedForward({0,0})[1])
print("input (1 0) | "..network:feedForward({1,0})[1])
print("input (0 1) | "..network:feedForward({0,1})[1])
print("input (1 1) | "..network:feedForward({1,1})[1])
- Library -
local nn = {}
nn.__index = nn
nn.ActivationFunctions = {
sigmoid = function(x) return 1/(1+math.exp(-x/1)) end,
ReLu = function(x) return math.max(0, x) end,
}
nn.Derivatives = {
sigmoid = function(x) return x * (1 - x) end,
ReLu = function(x) return x > 0 and 1 or 0 end,
}
nn.CostFunctions = {
MSE = function(outputs, expected)
local sum = 0
for i = 1, #outputs do
sum += 1/2*(expected[i] - outputs[i])^2
end
return sum/#outputs
end,
}
function nn.new(inputs, outputs, hiddenLayers, neurons, learningRate, activationFunction)
local self = setmetatable({}, nn)
self.learningRate = learningRate or .3
self.activationFunction = activationFunction or "ReLu"
self.net = {}
local net = self.net
local layers = hiddenLayers+2
for i = 1, layers do
net[i] = {}
end
for i = 1, inputs do
net[1][i] = {value = 0}
end
for i = 2, layers-1 do
for x = 1, neurons do
net[i][x] = {netInput = 0, value = 0, bias = math.random()*2-1, weights = {}}
for z = 1, #net[i-1] do
net[i][x].weights[z] = math.random()*2-1
end
end
end
for i = 1, outputs do
net[layers][i] = {netInput = 0, value = 0, bias = math.random()*2-1, weights = {}}
for z = 1, #net[layers-1] do
net[layers][i].weights[z] = math.random()*2-1
end
end
return self
end
function nn.newFromRawData(data)
return setmetatable(data, nn)
end
function nn:feedForward(inputs)
local net = self.net
local activation = self.activationFunction
local layers = #net
local inputLayer = net[1]
local outputLayer = net[layers]
for i = 1, #inputLayer do
inputLayer[i].value = inputs[i]
end
for i = 2, layers do
local layer = net[i]
for x = 1, #layer do
local sum = layer[x].bias
for z = 1, #net[i-1] do
sum += net[i-1][z].value * layer[x].weights[z]
end
layer[x].netInput = sum
layer[x].value = nn.ActivationFunctions[activation](sum)
end
end
local outputs = {}
for i = 1, #outputLayer do
table.insert(outputs, outputLayer[i].value)
end
return outputs
end
function nn:backPropagate(inputs, expected)
local outputs = self:feedForward(inputs)
local net = self.net
local activation = self.activationFunction
local layers = #net
local lr = self.learningRate
local inputLayer = net[1]
local outputLayer = net[layers]
for i = 1, #outputLayer do
local delta = -(expected[i] - outputs[i]) * nn.Derivatives[activation](net[layers][i].value)
outputLayer[i].delta = delta
end
for i = layers-1, 2, -1 do
local layer = net[i]
local nextLayer = net[i+1]
for x = 1, #layer do
local delta = 0
for z = 1, #nextLayer do
delta += nextLayer[z].delta * nextLayer[z].weights[x]
end
layer[x].delta = delta * nn.Derivatives[activation](layer[x].value)
end
end
for i = 2, layers do
local lastLayer = net[i-1]
for x = 1, #net[i] do
net[i][x].bias -= lr * net[i][x].delta
for z = 1, #lastLayer do
net[i][x].weights[z] -= lr * net[i][x].delta * lastLayer[z].value
end
end
end
end
return nn
Any help would be highly appreciated, thanks!
All initial weights must be DIFFERENT numbers, otherwise backpropagation will not work. For example, you can replace 1 with math.random()
Increase number of attempts to 10000
With these modifications, your code works fine:
Results:
input (0 0) | 0.028138230938126
input (1 0) | 0.97809448578087
input (0 1) | 0.97785000216126
input (1 1) | 0.023128477689456
I'm trying to plot different paths at rviz
I'm using the following code to get a first approach (based on this repository: https://github.com/HaoQChen/show_trajectory/tree/master/src)
import rospy
import math
import numpy as np
from geometry_msgs.msg import PoseStamped
from nav_msgs.msg import Path, Odometry
from std_msgs.msg import Empty
class ProjectElement(object):
def __init__(self):
self.path_pub = rospy.Publisher('~path', Path, latch=True, queue_size=10)
self.circle_sub = rospy.Subscriber('~circle', Empty, self.circle_cb, queue_size=10)
self.line_sub = rospy.Subscriber('~line', Empty, self.line_cb, queue_size=10)
self.project_sub = rospy.Subscriber('~project', Empty, self.project_cb, queue_size=10)
self.paths = []
self.rate = rospy.Rate(50)
def circle_cb(self, msg):
path = Path()
centre_x = 1
centre_y = 1
R = 0.5
th = 0.0
delta_th = 0.1
while (th<2*math.pi):
x = centre_x + R * math.sin(th)
y = centre_y + R * math.cos(th)
th += delta_th
this_pose_stamped = PoseStamped()
this_pose_stamped.pose.position.x = x
this_pose_stamped.pose.position.y = y
this_pose_stamped.header.stamp = rospy.get_rostime()
this_pose_stamped.header.frame_id = "/my_cs"
path.poses.append(this_pose_stamped)
path.header.frame_id = "/my_cs"
path.header.stamp = rospy.get_rostime()
self.paths.append(path)
def line_cb(self, msg):
path = Path()
x_start = 0.0
y_start = 0.0
length = 2
angle = 45 * math.pi/180
th = 0.0
delta_th = 0.1
while (th<length):
x = x_start + th * math.cos(angle)
y = y_start + th * math.sin(angle)
th += delta_th
this_pose_stamped = PoseStamped()
this_pose_stamped.pose.position.x = x
this_pose_stamped.pose.position.y = y
this_pose_stamped.header.stamp = rospy.get_rostime()
this_pose_stamped.header.frame_id = "/my_cs"
path.poses.append(this_pose_stamped)
path.header.frame_id = "/my_cs"
path.header.stamp = rospy.get_rostime()
self.paths.append(path)
def project_cb(self, msg):
while(True):
for element in self.paths:
# element.header.stamp = rospy.get_rostime()
self.path_pub.publish(element)
if __name__ == '__main__':
rospy.init_node('path_simulate')
elements = ProjectElement()
rospy.spin()
I can visualize the paths at rviz, but I don't know how to plot both figures at the same time in this way.
line
circle
I would like to ask if this approach is the best way to address this issue or which could be the best way.
I finally solved with visualization_msgs/MarkerArray based on this question:
https://answers.ros.org/question/220898/how-to-use-rviz-to-show-multiple-planing-paths/?answer=220993#post-id-220993
I post the code used here in case anyone needs it
import rospy
import math
import numpy as np
from geometry_msgs.msg import Vector3, Point
from std_msgs.msg import Empty
from visualization_msgs.msg import Marker, MarkerArray
class ProjectElement(object):
def __init__(self):
self.marker_pub = rospy.Publisher('~marker', MarkerArray, latch=True, queue_size=10)
self.circle_sub = rospy.Subscriber('~circle', Empty, self.circle_cb, queue_size=10)
self.line_sub = rospy.Subscriber('~line', Empty, self.line_cb, queue_size=10)
self.project_sub = rospy.Subscriber('~project', Empty, self.project_cb, queue_size=10)
self.marker_array = MarkerArray()
def circle_cb(self, msg):
marker = Marker()
marker.type = Marker.LINE_STRIP
marker.action = Marker.ADD
marker.scale = Vector3(0.01, 0.01, 0)
marker.color.g = 1.0
marker.color.a = 1.0
centre_x = 1
centre_y = 1
R = 0.5
delta_th = 0.1
for th in np.arange(0.0, 2*math.pi+delta_th, delta_th):
x = centre_x + R * math.sin(th)
y = centre_y + R * math.cos(th)
point = Point()
point.x = x
point.y = y
marker.points.append(point)
marker.id = 0
marker.header.stamp = rospy.get_rostime()
marker.header.frame_id = "/my_cs"
self.marker_array.markers.append(marker)
def line_cb(self, msg):
marker = Marker()
marker.type = Marker.LINE_STRIP
marker.action = Marker.ADD
marker.scale = Vector3(0.01, 0.01, 0)
marker.color.g = 1.0
marker.color.a = 1.0
x_start = 0.0
y_start = 0.0
length = 2
angle = 45 * math.pi/180
delta_th = 0.1
for th in np.arange(0.0, length, delta_th):
x = x_start + th * math.cos(angle)
y = y_start + th * math.sin(angle)
point = Point()
point.x = x
point.y = y
marker.points.append(point)
marker.id = 1
marker.header.stamp = rospy.get_rostime()
marker.header.frame_id = "/my_cs"
self.marker_array.markers.append(marker)
def project_cb(self, msg):
self.marker_pub.publish(self.marker_array)
if __name__ == '__main__':
rospy.init_node('markers_simulate')
elements = ProjectElement()
rospy.spin()
And the result achieved
marker array display
I need to train a MASK-RCNN. But when I start the training, I got the following error message:
ERROR:root:Error processing image {'id': 'ISIC_0010064.jpg', 'source': 'lesion', 'path': '/home/mine/Desktop/ISIC2018/ISIC2018_inputs/ISIC_0010064.jpg'}
Traceback (most recent call last):
File "/home/mine/.virtualenvs/cv/lib/python3.6/site-packages/mask_rcnn-2.1-py3.6.egg/mrcnn/model.py", line 1709, in data_generator
use_mini_mask=config.USE_MINI_MASK)
File "/home/mine/.virtualenvs/cv/lib/python3.6/site-packages/mask_rcnn-2.1-py3.6.egg/mrcnn/model.py", line 1265, in load_image_gt
class_ids = class_ids[_idx]
IndexError: invalid index to scalar variable.
I've already changed the number of classes, change more parameters in config, but the error persists.
There is my code:
DATASET_PATH = "/home/enacom/Desktop/ISIC2018"
IMAGES_PATH = os.path.join(DATASET_PATH, "ISIC2018_inputs")
MASKS_PATH = os.path.join(DATASET_PATH, "ISIC2018_ground_truth")
IMAGES_PATH = sorted(list(paths.list_images(IMAGES_PATH)))
idxs = list(range(0, len(IMAGES_PATH)))
random.seed(42)
random.shuffle(idxs)
i = int(len(idxs) * 0.8)
trainIdxs = idxs[:i]
valIdxs = idxs[i:]
CLASS_NAMES = {1: "lesion"}
COCO_PATH = "mask_rcnn_coco.h5"
LOGS_AND_MODEL_DIR = "lesion_logs"
class LesionBoundaryConfig(Config):
NAME = "lesion"
GPU_COUNT = 1
IMAGES_PER_GPU = 1
STEPS_PER_EPOCH = len(trainIdxs)
VALIDATION_STEPS = len(valIdxs) # doesnt suport low values
NUM_CLASSES = len(CLASS_NAMES) + 1
DETECTION_MIN_CONFIDENCE = 0.75
IMAGE_MIN_DIM = 128
IMAGE_MAX_DIM = 1024
class LesionBoundaryDataset(Dataset):
def __init__(self, imagePaths, classNames, width = 1024):
super().__init__(self)
self.imagePaths = imagePaths
self.classNames = classNames
self.width = width
def load_lesions(self, idxs):
for (classID, label) in self.classNames.items():
self.add_class("lesion", classID, label)
for i in idxs:
imagePath = self.imagePaths[i]
filename = imagePath.split(os.path.sep)[-1]
self.add_image("lesion", image_id=filename, path = imagePath)
def load_image(self, image_ID):
p = self.image_info[image_ID]["path"]
image = cv2.imread(p)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = imutils.resize(image, width=self.width, height=self.width)
return image
def load_mask(self, image_id):
info = self.image_info[image_id]
filename = info["id"].split(".")[0]
annot_path = os.path.sep.join([MASKS_PATH, "{}_segmentation.png".format(filename)])
annot_mask = cv2.imread(annot_path)
annot_mask = cv2.split(annot_mask)[0]
annot_mask = imutils.resize(annot_mask, width=self.width, inter = cv2.INTER_NEAREST)
annot_mask[annot_mask > 0] = 1
# function to take unique ids
class_ids = np.unique(annot_mask)
# remove the id 0 because we should ignore the background
class_ids = np.delete(class_ids, [0])
masks = np.zeros((annot_mask.shape[0], annot_mask.shape[1], 1),
dtype="uint8")
for (i, class_ids) in enumerate(class_ids):
class_mask = np.zeros(annot_mask.shape, dtype="uint8")
class_mask[annot_mask == class_ids] = 1
masks[:, :, i] = class_mask
return (masks.astype("bool"), class_ids.astype("int32"))
mode = "training"
train_dataset = LesionBoundaryDataset(IMAGES_PATH, CLASS_NAMES)
train_dataset.load_lesions(trainIdxs)
train_dataset.prepare()
val_dataset = LesionBoundaryDataset(IMAGES_PATH, CLASS_NAMES)
val_dataset.load_lesions(valIdxs)
val_dataset.prepare()
config = LesionBoundaryConfig()
config.display()
aug = iaa.SomeOf((0, 2), [
iaa.Fliplr(0.5),
iaa.Fliplr(0.5),
iaa.Affine(rotate=(-10, 10))
])
model = MaskRCNN(mode, config = config, model_dir=LOGS_AND_MODEL_DIR)
model.load_weights(COCO_PATH, by_name=True, exclude=["mrcnn_class_logits", "mrcnn_bbox_fc","mrcnn_bbox", "mrcnn_mask"])
model.train(train_dataset, val_dataset, epochs=20,
layers="heads", learning_rate=config.LEARNING_RATE /10, augmentation=aug)
I just want a resolution to make my training works. I've searched before post here, but I couldn't found any solution.
Im using Dijkstra algorythm code from this site: https://rosettacode.org/wiki/Dijkstra%27s_algorithm#Lua
Unfortunately It doesnt work for current edges table.
I have determined that the problem disappears when I delete the connection from 35 -> 36, but It doesnt solve the problem.
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
-- Fill in paths in the opposite direction to the stated edges
function complete (graph)
for node, edges in pairs(graph) do
for edge, distance in pairs(edges) do
if not graph[edge] then graph[edge] = {} end
graph[edge][node] = distance
end
end
end
-- Create path string from table of previous nodes
function follow (trail, destination)
local path, nextStep = destination, trail[destination]
while nextStep do
path = nextStep .. " " .. path
nextStep = trail[nextStep]
end
return path
end
-- Find the shortest path between the current and destination nodes
function dijkstra (graph, current, destination, directed)
if not directed then complete(graph) end
local unvisited, distanceTo, trail = {}, {}, {}
local nearest, nextNode, tentative
for node, edgeDists in pairs(graph) do
if node == current then
distanceTo[node] = 0
trail[current] = false
else
distanceTo[node] = math.huge
unvisited[node] = true
end
end
repeat
nearest = math.huge
for neighbour, pathDist in pairs(graph[current]) do
if unvisited[neighbour] then
tentative = distanceTo[current] + pathDist
if tentative < distanceTo[neighbour] then
distanceTo[neighbour] = tentative
trail[neighbour] = current
end
if tentative < nearest then
nearest = tentative
nextNode = neighbour
end
end
end
unvisited[current] = false
current = nextNode
until unvisited[destination] == false or nearest == math.huge
return distanceTo[destination], follow(trail, destination)
end
-- Main procedure
print("Directed:", dijkstra(edges, 34, 38, true))
print("Undirected:", dijkstra(edges, 34, 38, false))
I recieve the output of inf, 38 with current egdes table content but when I delete the connection between 35 -> 36 it gives an good output - 3, 34 35 46 38
For easier understand im uploading the graphic representation of edges table: https://i.imgur.com/FFF22C1.png
As you can see the route is corrent when we start from 34 -> 35 -> 46 -> 38 but as I sad It works only when connection from 35 to 36 is not existing.
Why it is not working in the case showed in my code?
This is an example of Dijkstra algorithm implementation
-- Graph definition
local edges = {
[34] = {[35] = 1,[37] = 1,},
[35] = {[34] = 1,[36] = 1,[46] = 1,},
[36] = {[35] = 1,[37] = 1,},
[37] = {[34] = 1,[36] = 1,},
[38] = {[46] = 1,},
[46] = {[35] = 1,[38] = 1,},
}
local starting_vertex, destination_vertex = 34, 38
local function create_dijkstra(starting_vertex)
local shortest_paths = {[starting_vertex] = {full_distance = 0}}
local vertex, distance, heap_size, heap = starting_vertex, 0, 0, {}
return
function (adjacent_vertex, edge_length)
if adjacent_vertex then
-- receiving the information about adjacent vertex
local new_distance = distance + edge_length
local adjacent_vertex_info = shortest_paths[adjacent_vertex]
local pos
if adjacent_vertex_info then
if new_distance < adjacent_vertex_info.full_distance then
adjacent_vertex_info.full_distance = new_distance
adjacent_vertex_info.previous_vertex = vertex
pos = adjacent_vertex_info.index
else
return
end
else
adjacent_vertex_info = {full_distance = new_distance, previous_vertex = vertex, index = 0}
shortest_paths[adjacent_vertex] = adjacent_vertex_info
heap_size = heap_size + 1
pos = heap_size
end
while pos > 1 do
local parent_pos = (pos - pos % 2) / 2
local parent = heap[parent_pos]
local parent_info = shortest_paths[parent]
if new_distance < parent_info.full_distance then
heap[pos] = parent
parent_info.index = pos
pos = parent_pos
else
break
end
end
heap[pos] = adjacent_vertex
adjacent_vertex_info.index = pos
elseif heap_size > 0 then
-- which vertex neighborhood to ask for?
vertex = heap[1]
local parent = heap[heap_size]
heap[heap_size] = nil
heap_size = heap_size - 1
if heap_size > 0 then
local pos = 1
local last_node_pos = heap_size / 2
local parent_info = shortest_paths[parent]
local parent_distance = parent_info.full_distance
while pos <= last_node_pos do
local child_pos = pos + pos
local child = heap[child_pos]
local child_info = shortest_paths[child]
local child_distance = child_info.full_distance
if child_pos < heap_size then
local child_pos2 = child_pos + 1
local child2 = heap[child_pos2]
local child2_info = shortest_paths[child2]
local child2_distance = child2_info.full_distance
if child2_distance < child_distance then
child_pos = child_pos2
child = child2
child_info = child2_info
child_distance = child2_distance
end
end
if child_distance < parent_distance then
heap[pos] = child
child_info.index = pos
pos = child_pos
else
break
end
end
heap[pos] = parent
parent_info.index = pos
end
local vertex_info = shortest_paths[vertex]
vertex_info.index = nil
distance = vertex_info.full_distance
return vertex
end
end,
shortest_paths
end
local vertex, dijkstra, shortest_paths = starting_vertex, create_dijkstra(starting_vertex)
while vertex and vertex ~= destination_vertex do
-- send information about all adjacent vertexes of "vertex"
for adjacent_vertex, edge_length in pairs(edges[vertex]) do
dijkstra(adjacent_vertex, edge_length)
end
vertex = dijkstra() -- now dijkstra is asking you about the neighborhood of another vertex
end
if vertex then
local full_distance = shortest_paths[vertex].full_distance
local path = vertex
while vertex do
vertex = shortest_paths[vertex].previous_vertex
if vertex then
path = vertex.." "..path
end
end
print(full_distance, path)
else
print"Path not found"
end
I'm beginner in tensorflow and i'm working on a model which Colorize Greyscale images but when i run the optmizer it give the same Error (MSE) every epoch and i can't figure out what is the error, so what is wrong with my code , what am i missing?
The logic: I get the low level and global and mid level features from the image and pass the global Features to multilayer function and fuse it's output with the global part in a one fusion layer and send the fused features vector to the colorization network ,, and i have Get_images_chrominance function which get the a,b values from the labels images and store them to feed the lables with them.
The Code
Ab_values = None
Batch_size = 3
Batch_indx = 1
Batch_GreyImages = []
Batch_ColorImages = []
EpochsNum = 11
ExamplesNum = 9
Imgsize = 224, 224
Channels = 1
Input_images = tf.placeholder(dtype=tf.float32,shape=[None,224,224,1])
Ab_Labels_tensor = tf.placeholder(dtype=tf.float32,shape=[None,224,224,2])
def ReadNextBatch():
global Batch_GreyImages
global Batch_ColorImages
global Batch_indx
global Batch_size
global Ab_values
Batch_GreyImages = []
Batch_ColorImages = []
for ind in range(Batch_size):
Colored_img = Image.open(r'Path' + str(Batch_indx) + '.jpg')
Batch_ColorImages.append(Colored_img)
Grey_img = Image.open(r'Path' + str(Batch_indx) + '.jpg')
Grey_img = np.asanyarray(Grey_img)
img_shape = Grey_img.shape
img_reshaped = Grey_img.reshape(img_shape[0],img_shape[1], Channels)#[224,224,1]
Batch_GreyImages.append(img_reshaped)#[#imgs,224,224,1]
Batch_indx = Batch_indx + 1
Get_Images_Chrominance()
return Batch_GreyImages
#-------------------------------------------------------------------------------
def Get_Images_Chrominance():
global Ab_values
global Batch_ColorImages
Ab_values = np.empty((Batch_size,224,224,2),"float32")
for indx in range(Batch_size):
lab = color.rgb2lab(Batch_ColorImages[indx])
for i in range(len(lab[:,1,1])):
for j in range(len(lab[1,:,1])):
Ab_values[indx][i][j][0] = lab[i,j,1]
Ab_values[indx][i][j][1] = lab[i,j,2]
min_value = np.amin(Ab_values[indx])
max_value = np.amax(Ab_values[indx])
for i in range(len(lab[:,1,1])):
for j in range(len(lab[1,:,1])):
Ab_values[indx][i][j][0] = Normalize(lab[i,j,1],min_value,max_value)
Ab_values[indx][i][j][1] = Normalize(lab[i,j,1],min_value,max_value)
#-------------------------------------------------------------------------------
def Normalize(value,min_value,max_value):
min_norm_value = 0
max_norm_value = 1
value = min_norm_value + (((max_norm_value - min_norm_value) * (value - min_value)) / (max_value - min_value))
return value
def Frobenius_Norm(M):
return tf.reduce_sum(M ** 2) ** 0.5
def Model(Input_images):
low_layer1 = ConstructLayer(Input_images,1,3,3,64,2,'Relu')
low_layer2 = ConstructLayer(low_layer1,64,3,3,128,1,'Relu')
low_layer3 = ConstructLayer(low_layer2,128,3,3,128,2,'Relu')
low_layer4 = ConstructLayer(low_layer3,128,3,3,256,1,'Relu')
low_layer5 = ConstructLayer(low_layer4,256,3,3,256,2,'Relu')
low_layer6 = ConstructLayer(low_layer5,256,3,3,512,1,'Relu')
mid_layer1 = ConstructLayer(low_layer6,512,3,3,512,1,'Relu')
mid_layer2 = ConstructLayer(mid_layer1,512,3,3,256,1,'Relu')
global_layer1 = ConstructLayer(low_layer6,512,3,3,512,2,'Relu')
global_layer2 = ConstructLayer(global_layer1,512,3,3,512,1,'Relu')
global_layer3 = ConstructLayer(global_layer2,512,3,3,512,2,'Relu')
global_layer4 = ConstructLayer(global_layer3,512,3,3,512,1,'Relu')
ML_Net = ConstructML(global_layer4,3,[1024,512,256])
Fuse = Fusion_layer(mid_layer2, ML_OUTPUT)
Color_layer1 = ConstructLayer(Fuse,256,3,3,128,1,'Relu')
Color_layer1 = UpSampling(56,56,Color_layer1)
Color_layer2 = ConstructLayer(Color_layer1,128,3,3,64,1,'Relu')
Color_layer3 = ConstructLayer(Color_layer2,64,3,3,64,1,'Relu')
Color_layer3 = UpSampling(112,112,Color_layer3)
Color_layer4 = ConstructLayer(Color_layer3,64,3,3,32,1,'Relu')
Output = ConstructLayer(Color_layer4,32,3,3,2,1,'Sigmoid')
Output = UpSampling(224,224,Output)
return Output
#----------------------------------------------------Training-------------------
def RunModel(Input_images):
global Ab_values
global Batch_indx
Prediction = Model(Input_images)
Colorization_MSE = tf.reduce_mean((Frobenius_Norm(tf.sub(Prediction,Ab_Labels_tensor))))
Optmizer = tf.train.AdadeltaOptimizer().minimize(Colorization_MSE)
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for epoch in range(EpochsNum):
epoch_loss = 0
Batch_indx = 1
for i in range(int(ExamplesNum / Batch_size)):#over batches
print("Batch Num ",i+1)
ReadNextBatch()
_, c = sess.run([Optmizer,Colorization_MSE],feed_dict={Input_images:Batch_GreyImages,Ab_Labels_tensor:Ab_values})
epoch_loss += c
print("epoch: ",epoch+1, ",Los: ",epoch_loss)
#---- ---------------------------------------------------------------------------
RunModel(Input_images)
EDIT: this is the full code if any anyone want to help me in