Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/darknet #356

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

junjihashimoto
Copy link
Member

@junjihashimoto junjihashimoto commented Apr 17, 2020

  • Parser for darknet init file
  • [] Read darknet weight file
  • [] Implement yolov3

@@ -48,11 +51,36 @@ instance Parameterized Parameter where
flattenParameters = pure
replaceOwnParameters _ = nextParameter

instance Parameterized a => Parameterized [a] where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this instance maybe you can also use this generalized version (sorry I hadn't gotten around to fixing the other one there yet, but this one seems good).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your suggestion.
I've changed the instance with generalized one.
Untyped instance can be fixed with 'OVERLAPS' pragma.
But typed instance can not be fixed with 'OVERLAPS' pragma.(I don't know why.)

instance Parameterized Double where
flattenParameters _ = []
replaceOwnParameters = return

instance Parameterized [Int] where
instance Parameterized Float where
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this one I tried instance (Scalar a) => Parameterized a, but GHC felt that was not okay. 😕

@@ -153,7 +178,7 @@ data Conv2d =
Conv2d {
conv2dWeight :: Parameter,
conv2dBias :: Parameter
} deriving (Show, Generic)
} deriving (Show, Generic, Parameterized)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can do that now? that's great! :)

let [a,b] = asValue v
in (a,b)
_dtype = error "Not implemented for tuple-type"
_dims v = error "Not implemented for tuple-type"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[2]?

let pad = (layerSize - 1) `div` 2
activation = if isLeaky then flip I.leaky_relu 0.1 else id
in activation
$ batchNormForward batchNorm train 0.90000000000000002 1.0000000000000001e-05
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers 😾

@junjihashimoto
Copy link
Member Author

@tycho01 Thank you for your reviewing.

Int ->
-- | [1, 1, grid, grid]
Tensor
gridY g = D.contiguous $ D.view [1, 1, g, g] $ I.t $ D.repeat [g, 1] $ arange' (0 :: Int) g (1 :: Int)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +476 to +484
loadWeights :: Darknet -> String -> IO Darknet
loadWeights (Darknet layers) weights_file = do
System.IO.withFile weights_file System.IO.ReadMode $ \handle -> do
_ <- BS.hGet handle (5 * 4) -- skip header
layers' <- forM layers $ \(i,layer) -> do
let cur_params = flattenParameters layer
new_params <- forM cur_params $ \param -> loadFloats handle (toDependent param) >>= makeIndependent
return $ (i,replaceParameters layer new_params)
return $ Darknet layers'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conv2d/BatchNorm's paramaters are loaded incorrectly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any idea why? I would like to try to do some work on loading pytorch models in the near future as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data is stored in the weight file as follows.
1, Bias of batchnorm
2, Weight of batchnorm
3, RunningMean of batchnorm
4, RunningVar of batchnorm
5, Weight of Conv2d

Conv2d has no bias.
https://github.com/eriklindernoren/PyTorch-YOLOv3/blob/master/models.py#L282-L317

The list of flattenParameters includes the bias of Conv2d with batchnorm.
Conv2d without batchnorm has bias.
Loader customization is needed.
 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be easier to fix the ADT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants